Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the last node of the LinkedList in C#
In C#, you can get the last node of a LinkedList<T> using the Last property. This property returns a LinkedListNode<T> object representing the last node in the list, or null if the list is empty.
Syntax
Following is the syntax to get the last node of a LinkedList −
LinkedListNode<T> lastNode = linkedList.Last; T lastValue = linkedList.Last.Value;
Using Last Property with String LinkedList
The following example demonstrates how to access the last node in a string LinkedList −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
list.AddLast("A");
list.AddLast("B");
list.AddLast("C");
list.AddLast("D");
list.AddLast("E");
list.AddLast("F");
list.AddLast("G");
list.AddLast("H");
list.AddLast("I");
list.AddLast("J");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("First Node = " + list.First.Value);
Console.WriteLine("Last Node = " + list.Last.Value);
list.Clear();
Console.WriteLine("Count of nodes (updated) = " + list.Count);
}
}
The output of the above code is −
Count of nodes = 10 First Node = A Last Node = J Count of nodes (updated) = 0
Using Last Property with Integer LinkedList
Here's another example using an integer LinkedList −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
list.AddLast(400);
list.AddLast(500);
list.AddLast(600);
list.AddLast(700);
list.AddLast(800);
list.AddLast(900);
list.AddLast(1000);
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("First Node = " + list.First.Value);
Console.WriteLine("Last Node = " + list.Last.Value);
list.Clear();
Console.WriteLine("Count of nodes (updated) = " + list.Count);
}
}
The output of the above code is −
Count of nodes = 10 First Node = 100 Last Node = 1000 Count of nodes (updated) = 0
Handling Empty LinkedList
When working with the Last property, it's important to check if the LinkedList is empty to avoid null reference exceptions −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
// Check if list is empty before accessing Last
if (list.Last != null) {
Console.WriteLine("Last Node = " + list.Last.Value);
} else {
Console.WriteLine("LinkedList is empty");
}
// Add some elements
list.AddLast("First");
list.AddLast("Second");
list.AddLast("Third");
// Now safe to access Last
Console.WriteLine("Last Node = " + list.Last.Value);
}
}
The output of the above code is −
LinkedList is empty Last Node = Third
Key Points
-
The
Lastproperty has O(1) time complexity as LinkedList maintains references to both first and last nodes. -
Lastreturnsnullif the LinkedList is empty. -
Use
Last.Valueto get the actual data stored in the last node. -
Always check for null before accessing
Last.Valueto avoid exceptions.
Conclusion
The Last property provides an efficient way to access the last node of a LinkedList in C#. Always verify that the LinkedList is not empty before accessing the Value property to prevent null reference exceptions.
