C# | Removing all nodes from LinkedList<T> Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share 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://docs.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 all nodes from LinkedList<T> 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# | 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