C# | Check if a value is in LinkedList<T>
LinkedList<T>.Contains(T) method is used to check whether a value is in the LinkedList<T> or not. Syntax:
public bool Contains (T value);
Here, value is the value to locate in the LinkedList<T>. The value can be null for reference types. Return Value: This method returns True if value is found in the LinkedList, otherwise, False. Below given are some examples to understand the implementation in a better way: Example 1:
CSHARP
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList<String> myList = new LinkedList<String>();
myList.AddLast( "A" );
myList.AddLast( "B" );
myList.AddLast( "C" );
myList.AddLast( "D" );
myList.AddLast( "E" );
Console.WriteLine(myList.Contains( "B" ));
}
}
|
Output:
True
Example 2:
CSHARP
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList< int > myList = new LinkedList< int >();
myList.AddLast(1);
myList.AddLast(2);
myList.AddLast(3);
myList.AddLast(4);
myList.AddLast(5);
Console.WriteLine(myList.Contains(8));
}
}
|
Output:
False
Note: This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count.
Space complexity: O(n) where n is size of the LinkedList
Reference:
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...