The LinkedList<T>
generic class in the System.Collections.Generic
namespace provides the FindLast()
method.
We can use this method to find the last node with a specified value in a linked list in C#
.
public System.Collections.Generic.LinkedListNode<T>? FindLast (T value);
It takes the T
value as input to be searched in the linked list.
It returns the last linked list node that matches the T
value in the linked list.
It returns null if no matching value is found during the search.
This method is an O(n) operation, as it performs a linear search in the linked list and may have to traverse all the elements in the worst case.
The search is done backward, starting from the last node and ending at the first node.
In the code below, we created a linked list of strings (monthList
) and added the names of few months to it. The linked list contains four strings - January
, March
, March
, and April
.
using System;using System.Collections.Generic;class LinkedListFind {static void Main() {LinkedList<string> monthList = new LinkedList<string>();monthList.AddLast("January");monthList.AddLast("March");monthList.AddLast("March");monthList.AddLast("April");Console.WriteLine("LinkedList Elements");Print(monthList);string searchMonth1 = "March", searchMonth2 = "December";LinkedListNode<string> result = monthList.FindLast(searchMonth1);Console.WriteLine($"Searching list for {searchMonth1}");if(result!=null) {Console.WriteLine($"Found a node with value {result.Value}, previous Node : {result.Previous.Value}");} else {Console.WriteLine($"No node found with {searchMonth1} value");}result = monthList.FindLast(searchMonth2);Console.WriteLine($"Searching list for {searchMonth2}");if(result!=null) {Console.WriteLine($"Found a node with value {result.Value}");} else {Console.WriteLine($"No node found with {searchMonth2} value");}}private static void Print(LinkedList<string> list) {foreach (var node in list) {Console.Write(node + ", ");}Console.WriteLine("\n");}}
After we initialize the LinkedList, we call the FindLast()
method string searchMonth1 = March
to find
in the list.
FindLast()
performs a linear search in the list, starting from the last node, and returns the first linked list node it finds with the March
value.
We can see in the output panel that the returned node has a March
value, and the previous node is also March
.
Next, we call the FindLast()
method with parameter searchMonth2 = December
to find in the list.
This searching takes a linear search time in the list and returns null
, as there is no node in the list with that value. This information is displayed in the output as well.
We created a helper method, Print()
, to display the linked list nodes.
Please note that the
LinkedList<T>
class is maintained internally as a doubly-linked list, andFindLast()
is an O(n) operation.
The program prints the following output and exits.
LinkedList Elements
January, March, March, April,
Searching list for March
Found a node with value March, previous Node: March
Searching list for December
No node found with December value