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 better way:
Example 1:
// 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:
// 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:
Recommended Posts:
- C# | Removing the specified node from the LinkedList<T>
- C# | Removing the node at the start of the LinkedList<T>
- C# | Removing all nodes from LinkedList<T>
- C# | Removing all the elements from the List
- C# | Removing the specified element from the List
- C# | Removing first occurrence of specified value from LinkedList<T>
- Removing the object from the top of the Stack in C#
- C# | Removing all entries from HybridDictionary
- C# | Removing all entries from the StringDictionary
- C# | Removing the specified key entry from HybridDictionary
- C# | Removing first occurrence of object from Collection<T>
- C# | Removing a range of elements from the List
- C# | Get the first node of the LinkedList<T>
- C# | Get the last node of the LinkedList<T>
- C# | Adding new node or value at the end of LinkedList<T>
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.



