C# | Removing the node at the end of LinkedList<T> Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 better way: Example 1: CSHARP // C# code to remove the node at // the end of 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"); // Displaying the nodes in LinkedList Console.WriteLine("The elements in LinkedList are : "); foreach(string str in myList) { Console.WriteLine(str); } // Removing the node at the end of LinkedList myList.RemoveLast(); // Displaying the nodes in LinkedList Console.WriteLine("The elements in LinkedList are : "); foreach(string str in myList) { Console.WriteLine(str); } } } Output: The elements in LinkedList are : A B C D E The elements in LinkedList are : A B C D Example 2: CSHARP // C# code to remove the node at // the end of 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>(); // Removing the node at the end of LinkedList // This should raise "InvalidOperationException" // as the LinkedList is empty myList.RemoveLast(); // Displaying the nodes in LinkedList Console.WriteLine("The elements in LinkedList are : "); foreach(int i in myList) { Console.WriteLine(i); } } } Runtime Error: Unhandled Exception: System.InvalidOperationException: The LinkedList is empty. Note: This method is an O(1) operation. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.removelast?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Removing the node at the end of LinkedList<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Generic-Namespace CSharp-LinkedList CSharp-LinkedList-Methods Similar Reads 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 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 all nodes from LinkedList<T> 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. 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