Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the first node in LinkedList containing the specified value in C#
The LinkedList<T>.Find() method in C# searches for the first node that contains the specified value and returns a LinkedListNode<T> object. If the value is not found, it returns null.
Syntax
Following is the syntax for the Find() −
public LinkedListNode<T> Find(T value);
Parameters
value − The value to locate in the LinkedList.
Return Value
Returns the first LinkedListNode<T> that contains the specified value, or null if the value is not found.
Using Find() with String Values
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
list.AddLast("John");
list.AddLast("Tim");
list.AddLast("Kevin");
list.AddLast("Jacob");
list.AddLast("Emma");
list.AddLast("Ryan");
list.AddLast("Brad");
list.AddLast("Carl");
Console.WriteLine("LinkedList elements...");
foreach(string str in list) {
Console.WriteLine(str);
}
LinkedListNode<string> val = list.Find("Jacob");
Console.WriteLine("Specified value = " + val.Value);
}
}
The output of the above code is −
LinkedList elements... John Tim Kevin Jacob Emma Ryan Brad Carl Specified value = Jacob
Using Find() with Integer Values
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
list.AddLast(400);
list.AddLast(500);
Console.WriteLine("LinkedList elements...");
foreach(int i in list) {
Console.WriteLine(i);
}
LinkedListNode<int> val = list.Find(300);
Console.WriteLine("Specified value = " + val.Value);
}
}
The output of the above code is −
LinkedList elements... 100 200 300 400 500 Specified value = 300
Handling Non-Existent Values
When the specified value is not found, the Find() method returns null. Always check for null before accessing the Value property −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
list.AddLast("Apple");
list.AddLast("Banana");
list.AddLast("Cherry");
// Search for existing value
LinkedListNode<string> found = list.Find("Banana");
if (found != null) {
Console.WriteLine("Found: " + found.Value);
}
// Search for non-existing value
LinkedListNode<string> notFound = list.Find("Orange");
if (notFound == null) {
Console.WriteLine("Orange not found in the list");
}
}
}
The output of the above code is −
Found: Banana Orange not found in the list
Conclusion
The LinkedList<T>.Find() method efficiently locates the first node containing a specified value and returns the corresponding LinkedListNode<T> object. Always check for null return values to avoid runtime exceptions when the value is not found in the list.
