Open In App

C# | Get the last node of the LinkedList<T>

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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");
    }
}


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");
    }
}


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:



Similar Reads

C# | Find the last node in LinkedList&lt;T&gt; containing the specified value
LinkedList&lt;T&gt;.FindLast(T) method is used to find the last node that contains the specified value. Syntax: public System.Collections.Generic.LinkedListNode&lt;T&gt; FindLast (T value); Here, value is the value to locate in the LinkedList. Return Value: This method returns the last LinkedListNode&lt;T&gt; that contains the specified value, if f
2 min read
C# | Get the first node of the LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.First property is used to get the first node of the LinkedList&lt;T&gt;. Syntax: public System.Collections.Generic.LinkedListNode First { get; } Return Value: The first LinkedListNode&lt;T&gt; of the LinkedList&lt;T&gt;. Below given are some examples to understand the implementation in a better way: Example 1: // C# code to get
2 min read
C# | Get the number of nodes contained in LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.Count property is used to get the number of nodes actually contained in the LinkedList&lt;T&gt;. Syntax: public int Count { get; } Return Value: The number of nodes actually contained in the LinkedList. Note: Retrieving the value of this property is an O(1) operation. Below given are some examples to understand the implementatio
2 min read
C# | Removing the node at the start of the LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.RemoveFirst method is used to remove the node at the start of the LinkedList&lt;T&gt;. Syntax: public void RemoveFirst (); Exception: The method throws InvalidOperationException if the LinkedList&lt;T&gt; is empty. Below given are some examples to understand the implementation in a better way: Example 1: // C# code to remove the
2 min read
C# | Removing the node at the end of LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.RemoveLast method is used to remove the node at the end of the LinkedList&lt;T&gt;. Syntax: public void RemoveLast (); Exception: The method throws InvalidOperationException if the LinkedList&lt;T&gt; is empty. Below given are some examples to understand the implementation in a better way: Example 1: // C# code to remove the nod
2 min read
C# | Removing the specified node from the LinkedList&lt;T&gt;
Remove(LinkedListNode&lt;T&gt;) method is used to remove the specified node from the LinkedList&lt;T&gt;. Syntax: public void Remove (System.Collections.Generic.LinkedListNode&lt;T&gt; node); Here, node is the LinkedListNode&lt;T&gt; to remove from the LinkedList&lt;T&gt;. Exceptions: ArgumentNullException : If the node is null. InvalidOperationExc
2 min read
C# | Find the first node in LinkedList&lt;T&gt; containing the specified value
LinkedList&lt;T&gt;.Find(T) method is used to find the first node that contains the specified value. Syntax: public System.Collections.Generic.LinkedListNode&lt;T&gt; Find (T value); Here, value is the value to locate in the LinkedList. Return Value: This method returns the first LinkedListNode&lt;T&gt; that contains the specified value, if found,
2 min read
C# | Adding new node or value at the start of LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.AddFirst Method is used to add a new node or value at the starting of the LinkedList&lt;T&gt;. There are 2 methods in the overload list of this method as follows: AddFirst(LinkedList&lt;T&gt;) AddFirst(T) AddFirst(LinkedListNode&lt;T&gt;) This method is used to add the specified new node at the starting of the LinkedList&lt;T
3 min read
C# | Adding new node or value at the end of LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.AddLast Method is used to add a new node or value at the end of the LinkedList&lt;T&gt;. There are 2 methods in the overload list of this method as follows: AddLast(LinkedList&lt;T&gt;) AddLast(T) AddLast(LinkedListNode&lt;T&gt;) This method is used to add the specified new node at the end of the LinkedList&lt;T&gt;. Syntax: pub
3 min read
C# | Getting an enumerator that iterates through LinkedList&lt;T&gt;
LinkedList&lt;T&gt;.GetEnumerator Method is used to get an enumerator that iterates through the LinkedList&lt;T&gt;. Syntax: public System.Collections.Generic.LinkedList&lt;T&gt;.Enumerator GetEnumerator (); Return Value: It returns an LinkedList&lt;T&gt;.Enumerator for the LinkedList&lt;T&gt;. Below programs illustrate the use of above-discussed m
2 min read