LinkedList<T>.Find(T) method is used to find the first node that contains the specified value.
Syntax:
public System.Collections.Generic.LinkedListNode<T> Find (T value);
Here, value is the value to locate in the LinkedList.
Return Value: This method returns the first LinkedListNode<T> that contains the specified value, if found, otherwise, null.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to find the first node // that contains the specified value using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Strings LinkedList<String> myList = new LinkedList<String>(); // Adding nodes in LinkedList myList.AddLast("A"); myList.AddLast("B"); myList.AddLast("C"); myList.AddLast("D"); myList.AddLast("E"); // Finding the first node that // contains the specified value LinkedListNode<String> temp = myList.Find("B"); Console.WriteLine(temp.Value); } } |
Output:
B
Example 2:
// C# code to find the first node // that contains the specified value using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Integers LinkedList<int> myList = new LinkedList<int>(); // Adding nodes in LinkedList myList.AddLast(5); myList.AddLast(7); myList.AddLast(9); myList.AddLast(11); myList.AddLast(12); // Finding the first node that // contains the specified value LinkedListNode<int> temp = myList.Find(15); Console.WriteLine(temp.Value); } } |
Runtime Error:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
Note:
- The LinkedList is searched forward starting at First and ending at Last.
- This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Find the last node in LinkedList<T> containing the specified value
- C# | Removing first occurrence of specified value from LinkedList<T>
- C# | First occurrence in the List that matches the specified conditions
- Finding the Index of First Element of the Specified Sequence in C#
- C# | Get or Set the value associated with specified key in Hashtable
- C# | Set all bits in the BitArray to the specified value
- C# | Set the bit at a specific position in the BitArray to the specified value
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Add the specified key and value into the ListDictionary
- C# | Get or set the value associated with specified key in ListDictionary
- C# | Adding the specified key and value into HybridDictionary
- C# | Gets or sets the value in HybridDictionary with specified key
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | ArrayList whose elements are copies of the specified value
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Get or set the value associated with specified key in SortedList
- C# | Getting the value at the specified index of a SortedList object
- C# | Getting index of the specified value in a SortedList object
- C# | Gets or sets the value at the specified key in StringDictionary
- Getting the name of the Enumeration Constant having Specified value in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

