C# | Removing all nodes from LinkedList<T> Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report LinkedList<T>.Clear method is used to remove the all nodes from the LinkedList<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // nodes from 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); // Removing all nodes from LinkedList myList.Clear(); // To get the count of nodes in LinkedList // after removing all the nodes Console.WriteLine("Total nodes in myList are : " + myList.Count); } } Output: Total nodes in myList are : 5 Total nodes in myList are : 0 Example 2: CSHARP // C# code to remove all // nodes from 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); // To get the count of nodes in LinkedList // before removing all the nodes Console.WriteLine("Total nodes in myList are : " + myList.Count); // Removing all nodes from LinkedList myList.Clear(); // To get the count of nodes in LinkedList // after removing all the nodes Console.WriteLine("Total nodes in myList are : " + myList.Count); } } Output: Total nodes in myList are : 3 Total nodes in myList are : 0 Note: Count is set to zero, and references to other objects from elements of the collection are also released. First and Last are set to null. This method is an O(n) operation, where n is Count. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.clear?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Removing first occurrence of specified value from LinkedList S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Generic-Namespace CSharp-LinkedList CSharp-LinkedList-Methods Similar Reads C# | Removing the specified node from the LinkedList<T> 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: ArgumentNullEx 2 min read C# | Removing the node at the end of LinkedList<T> LinkedList<T>.RemoveLast method is used to remove the node at the end of the LinkedList<T>. Syntax: public void RemoveLast (); Exception: The method throws InvalidOperationException if the LinkedList<T> is empty. Below given are some examples to understand the implementation in a b 2 min read C# | Removing the node at the start of the LinkedList<T> LinkedList<T>.RemoveFirst method is used to remove the node at the start of the LinkedList<T>. Syntax: public void RemoveFirst (); Exception: The method throws InvalidOperationException if the LinkedList<T> is empty. Below given are some examples to understand the implementation in 2 min read C# | Removing first occurrence of specified value from LinkedList<T> Remove(T) method is used to remove the first occurrence of the specified value from the LinkedList<T>. Syntax: public bool Remove (T value); Here, value is the value to remove from the LinkedList<T>. Return Value: This method returns True if the element containing value is successfully r 3 min read C# | Adding new node or value at the end of LinkedList<T> LinkedList<T>.AddLast Method is used to add a new node or value at the end of the LinkedList<T>. There are 2 methods in the overload list of this method as follows: AddLast(LinkedList<T>) AddLast(T) AddLast(LinkedListNode<T>) This method is used to add the specified new node 3 min read C# | Adding new node or value at the start of LinkedList<T> LinkedList<T>.AddFirst Method is used to add a new node or value at the starting of the LinkedList<T>. There are 2 methods in the overload list of this method as follows: AddFirst(LinkedList<T>) AddFirst(T) AddFirst(LinkedListNode<T>) This method is used to add the specified 3 min read Like