Remove(LinkedListNode<T>) method is used to remove the specified node from the LinkedList<T>.
Syntax:
public void Remove (System.Collections.Generic.LinkedListNode<T> node);
Here, node is the LinkedListNode<T> to remove from the LinkedList<T>.
Exceptions:
- ArgumentNullException : If the node is null.
- InvalidOperationException : If the node is not in the current LinkedList<T>.
Below given are some examples to understand the implementation in a better way:
Example 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<int> myList = new LinkedList<int>(); // 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); } } } |
chevron_right
filter_none
Output:
Total nodes in myList are : 4 2 4 6 8 Total nodes in myList are : 3 4 6 8
Example 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<String> myList = new LinkedList<String>(); // 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); } } } |
chevron_right
filter_none
Output:
Total nodes in myList are : 5 A B C D E Total nodes in myList are : 4 A B C E
Note: This method is an O(1) operation.
Reference:
Recommended Posts:
- C# | Removing the specified element from the List
- C# | Removing the specified key entry from HybridDictionary
- C# | Removing first occurrence of specified value from LinkedList<T>
- C# | Find the first node in LinkedList<T> containing the specified value
- C# | Find the last node in LinkedList<T> containing the specified value
- C# | Removing the node at the start of the LinkedList<T>
- C# | Removing the node at the end of LinkedList<T>
- C# | Creating a HybridDictionary with specified initial size & case sensitivity
- C# | Creating an empty HybridDictionary with specified case sensitivity
- C# | Check if the specified string is in the StringCollection
- C# | Check if SortedSet and a specified collection share common elements
- C# | Check if HashSet and the specified collection contain the same elements
- C# | Insert an element into the ArrayList at the specified index
- C# | Check if a HashSet is a proper superset of the specified collection
- C# | Get or Set at specified index in StringCollection
- C# | Check if a HashSet and a specified collection share common elements
- C# | Check if a HashSet is a subset of the specified collection
- C# | Remove from the specified index of the StringCollection
- C# | Copy StringCollection at the specified index of array
- C# | Check if a HashSet contains the specified element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

