1.3.19

1.3.19 #

解答 #

建立一个结点引用 Cur,让它移动到尾结点的前一个结点,让那个结点的 next 变为 null。

代码 #

var first = new Node<string> { Item = "first" };
var second = new Node<string> { Item = "second" };
var third = new Node<string> { Item = "third" };

first.Next = second;
second.Next = third;
third.Next = null;

var current = first;
while (current != null)
{
    Console.Write(current.Item + " ");
    current = current.Next;
}

DeleteLast(first);
Console.WriteLine();

current = first;
while (current != null)
{
    Console.Write(current.Item + " ");
    current = current.Next;
}

Console.WriteLine();

static void DeleteLast(Node<string> first)
{
    var current = first;

    while (current.Next.Next != null)
    {
        current = current.Next;
    }

    current.Next = null;
}

另请参阅 #

Generics 库