Check if a value is in LinkedList in C#

To check if a value exists in a LinkedList in C#, you can use the Contains() method. This method performs a linear search through the linked list and returns true if the specified value is found, or false otherwise.

Syntax

Following is the syntax for the Contains() method −

public bool Contains(T item)

Parameters

  • item − The value to locate in the LinkedList.

Return Value

Returns true if the item is found in the LinkedList; otherwise, false.

LinkedList.Contains() Method Node 1 Node 2 Target Node 4 Linear search from head to tail Returns true when target is found

Using Contains() with Integer LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<int> linkedList = new LinkedList<int>();
      linkedList.AddLast(25);
      linkedList.AddLast(50);
      linkedList.AddLast(100);
      linkedList.AddLast(200);
      linkedList.AddLast(400);
      linkedList.AddLast(500);
      linkedList.AddLast(550);
      linkedList.AddLast(600);
      linkedList.AddLast(800);
      linkedList.AddLast(1200);

      Console.WriteLine("Count of nodes = " + linkedList.Count);
      foreach(int val in linkedList) {
         Console.WriteLine(val);
      }

      Console.WriteLine("Does the LinkedList has node 800? " + linkedList.Contains(800));
      Console.WriteLine("Does the LinkedList has node 999? " + linkedList.Contains(999));
   }
}

The output of the above code is −

Count of nodes = 10
25
50
100
200
400
500
550
600
800
1200
Does the LinkedList has node 800? True
Does the LinkedList has node 999? False

Using Contains() with String LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> linkedList = new LinkedList<string>();
      linkedList.AddLast("ABC");
      linkedList.AddLast("BCD");
      linkedList.AddLast("CDE");
      linkedList.AddLast("DEF");
      linkedList.AddLast("EFG");
      linkedList.AddLast("FGH");
      linkedList.AddLast("GHI");
      linkedList.AddLast("HIJ");
      linkedList.AddLast("IJK");

      Console.WriteLine("Count of nodes = " + linkedList.Count);
      foreach(string val in linkedList) {
         Console.WriteLine(val);
      }

      Console.WriteLine("Does the LinkedList has node 'GHI'? " + linkedList.Contains("GHI"));
      Console.WriteLine("Does the LinkedList has node 'KLM'? " + linkedList.Contains("KLM"));
   }
}

The output of the above code is −

Count of nodes = 9
ABC
BCD
CDE
DEF
EFG
FGH
GHI
HIJ
IJK
Does the LinkedList has node 'GHI'? True
Does the LinkedList has node 'KLM'? False

Performance Considerations

The Contains() method has O(n) time complexity because it performs a linear search through all nodes. For frequently accessed values, consider using a HashSet or Dictionary for O(1) lookup performance instead of a LinkedList.

Conclusion

The Contains() method provides a simple way to check if a value exists in a LinkedList in C#. It returns a boolean result after performing a linear search through the collection, making it suitable for scenarios where occasional lookups are needed on linked list data structures.

Updated on: 2026-03-17T07:04:36+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements