LinkedList<T>.Last property is used to get the last node of the LinkedList<T>.
Syntax:
public System.Collections.Generic.LinkedListNode Last { get; }
Return Value: The last LinkedListNode<T> of the LinkedList<T>.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to get the last // node 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"); // To get the last node of the LinkedList if (myList.Count > 0) Console.WriteLine(myList.Last.Value); else Console.WriteLine("LinkedList is empty"); } } |
chevron_right
filter_none
Output:
E
Example 2:
// C# code to get the last // node 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>(); // To get the last node of the LinkedList if (myList.Count > 0) Console.WriteLine(myList.Last.Value); else Console.WriteLine("LinkedList is empty"); } } |
chevron_right
filter_none
Output:
LinkedList is empty
Note:
- LinkedList accepts null as a valid Value for reference types and allows duplicate values.
- If the LinkedList is empty, the First and Last properties contain null.
- Retrieving the value of this property is an O(1) operation.
Reference:
Recommended Posts:
- C# | How to get the last occurrence of the element in the List that match the specified conditions
- C# | Find the last node in LinkedList<T> containing the specified value
- C# | Get the first node of the LinkedList<T>
- C# | Finding the index of last element in the array
- LINQ | Element Operator | Last
- Finding the Index which Points Beyond the Last Element in C#
- C# | How to get the HashCode of the tuple?
- C# | How to get a subset in a SortedSet
- C# | Get the maximum value in the SortedSet
- C# | How to get the Remaining Elements of the Tuple?
- C# | How to get TypeCode for the class String
- C# | Get the minimum value in the SortedSet
- C# | Get the number of elements actually contained in the ArrayList
- C# | Get the number of strings in StringCollection
- C# | Get or Set at specified index in StringCollection
- C# | Get the number of elements contained in the Stack
- C# | Get object at the top of the Stack - Peek operation
- C# | Get the number of elements contained in the Queue
- C# | Get the object at the beginning of the Queue - Peek Operation
- C# | Get or Set the value associated with specified key in Hashtable
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.

