How to find a node with a specified value in linked list in C#

The LinkedList<T> generic class in the System.Collections.Generic namespace provides the Find() method.

We can use this method to find the first node with the specified value in a linked list in C#.

Syntax


public System.Collections.Generic.LinkedListNode<T>? Find (T value);

Parameters and return type

  • It takes as input the value T to be searched in the linked list.

  • It returns the first linked list node matching the value T in the linked list.

  • It returns null if no matching value is found during the search.

Things to note

  • 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 forward, starting from the first node and ending at the last node.

Finding a node with specified value using Find(T) method

Code

In the below code, we created a linked list of strings (monthList) and added the names of few months to it. The linked list contains four strings: January, February, February, and April.

using System;
using System.Collections.Generic;
class LinkedListFind {
static void Main() {
LinkedList<string> monthList = new LinkedList<string>();
monthList.AddLast("January");
monthList.AddLast("February");
monthList.AddLast("February");
monthList.AddLast("April");
Console.WriteLine("LinkedList Elements");
Print(monthList);
string serachMonth1 = "February", serachMonth2 = "December";
LinkedListNode<string> result = monthList.Find(serachMonth1);
Console.WriteLine($"Searching list for {serachMonth1}");
if(result!=null) {
Console.WriteLine($"Found a node with value {result.Value}, previous Node : {result.Previous.Value}");
} else {
Console.WriteLine($"No node found with {serachMonth1} value");
}
result = monthList.Find(serachMonth2);
Console.WriteLine($"Searching list for {serachMonth2}");
if(result!=null) {
Console.WriteLine($"Found a node with value {result.Value}");
} else {
Console.WriteLine($"No node found with {serachMonth2} value");
}
}
private static void Print(LinkedList<string> list) {
foreach (var node in list) {
Console.Write(node + ", ");
}
Console.WriteLine("\n");
}
}

Explanation

After we initialize the linked list, we call the Find() method with the passing February string to find in the list.

The search performs a linear search (i.e., O(1)) in the list and returns the linked list node with the February value, and the previous node is January.

We also print the returned node value and its previous node value in the output.

Next, we call the Find() method by passing the input string as December to search in the list. It performs a linear search in the list and returns null as there is no node in the list with that value.

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, and Find() is an O(1) operation.

The program prints the output and exits.

Free Resources