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
C# program to find node in Linked List
The LinkedList<T> class in C# provides methods to search for nodes within the list. The Find() method returns the first LinkedListNode<T> that contains the specified value, or null if the value is not found.
Syntax
Following is the syntax for finding a node in a LinkedList −
LinkedListNode<T> node = linkedList.Find(value);
Following is the syntax for finding the last occurrence of a node −
LinkedListNode<T> node = linkedList.FindLast(value);
Return Value
The Find() method returns a LinkedListNode<T> object containing the value, or null if the value is not found. The FindLast() method works similarly but returns the last occurrence of the value.
Using Find() to Locate and Insert After a Node
Once you find a node, you can perform operations like adding elements before or after it −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<string> myList = new LinkedList<string>();
// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");
Console.WriteLine("Original List:");
foreach (var item in myList) {
Console.WriteLine(item);
}
// Find node with value "R"
LinkedListNode<string> node = myList.Find("R");
if (node != null) {
myList.AddAfter(node, "ADDED");
Console.WriteLine("\nAfter adding 'ADDED' after 'R':");
foreach (var item in myList) {
Console.WriteLine(item);
}
}
}
}
The output of the above code is −
Original List: P Q R S T U After adding 'ADDED' after 'R': P Q R ADDED S T U
Using Find() with Null Check
It's important to check if the node exists before performing operations on it −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddLast(30);
// Try to find existing and non-existing values
LinkedListNode<int> foundNode = numbers.Find(20);
LinkedListNode<int> notFoundNode = numbers.Find(50);
if (foundNode != null) {
Console.WriteLine("Found node with value: " + foundNode.Value);
}
if (notFoundNode == null) {
Console.WriteLine("Value 50 not found in the list");
}
}
}
The output of the above code is −
Found node with value: 20 Value 50 not found in the list
Using FindLast() Method
When a LinkedList contains duplicate values, FindLast() returns the last occurrence −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<string> fruits = new LinkedList<string>();
fruits.AddLast("Apple");
fruits.AddLast("Banana");
fruits.AddLast("Apple");
fruits.AddLast("Orange");
LinkedListNode<string> firstApple = fruits.Find("Apple");
LinkedListNode<string> lastApple = fruits.FindLast("Apple");
Console.WriteLine("First 'Apple' is at position: " + GetPosition(fruits, firstApple));
Console.WriteLine("Last 'Apple' is at position: " + GetPosition(fruits, lastApple));
}
static int GetPosition<T>(LinkedList<T> list, LinkedListNode<T> targetNode) {
int position = 0;
LinkedListNode<T> currentNode = list.First;
while (currentNode != null) {
if (currentNode == targetNode) {
return position;
}
currentNode = currentNode.Next;
position++;
}
return -1;
}
}
The output of the above code is −
First 'Apple' is at position: 0 Last 'Apple' is at position: 2
Conclusion
The Find() and FindLast() methods in C# LinkedList provide efficient ways to locate nodes containing specific values. Always check for null return values before performing operations on the found nodes to avoid runtime exceptions.
