📜  C#|从LinkedList中删除指定的节点<T>

📅  最后修改于: 2021-05-29 18:37:48             🧑  作者: Mango

Remove(LinkedListNode < T >)方法用于从LinkedList < T >中删除指定的节点。

句法:

public void Remove (System.Collections.Generic.LinkedListNode node);

在这里,节点是要从LinkedList < T >中删除的LinkedListNode < T >。

例外情况:

  • ArgumentNullException:如果节点为null。
  • InvalidOperationException:如果节点不在当前的LinkedList < T >中。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to remove the specified
// node from the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Integers
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast(2);
        myList.AddLast(4);
        myList.AddLast(6);
        myList.AddLast(8);
  
        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
  
        // Removing the first node from the LinkedList
        myList.Remove(myList.First);
  
        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
    }
}

输出:

Total nodes in myList are : 4
2
4
6
8
Total nodes in myList are : 3
4
6
8

范例2:

// C# code to remove the specified
// node from the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Strings
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast("A");
        myList.AddLast("B");
        myList.AddLast("C");
        myList.AddLast("D");
        myList.AddLast("E");
  
        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
  
        // Removing the specified node from the LinkedList
        myList.Remove("D");
  
        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
    }
}

输出:

Total nodes in myList are : 5
A
B
C
D
E
Total nodes in myList are : 4
A
B
C
E

注意:此方法是O(1)操作。

参考:

https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.linkedlist-1.remove?view=netframework-4.7.2#System_Collections_Generic_LinkedList_1_Remove_System_Collections_Generic_LinkedListNode__0__