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 last node in LinkedList containing the specified value in C#
The FindLast() method in C# LinkedList is used to find the last occurrence of a node containing the specified value. This method searches from the end of the LinkedList towards the beginning and returns the last LinkedListNode<T> that contains the specified value.
Syntax
Following is the syntax for the FindLast() method −
public LinkedListNode<T> FindLast(T value)
Parameters
value − The value to locate in the LinkedList.
Return Value
Returns the last LinkedListNode<T> that contains the specified value, or null if the value is not found.
Using FindLast() with Integer Values
The following example demonstrates finding the last occurrence of a duplicate value in a LinkedList of integers −
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);
list.AddLast(300);
list.AddLast(500);
Console.WriteLine("LinkedList elements...");
foreach(int i in list) {
Console.WriteLine(i);
}
LinkedListNode<int> val = list.FindLast(300);
Console.WriteLine("Specified value = " + val.Value);
}
}
The output of the above code is −
LinkedList elements... 100 200 300 400 500 300 500 Specified value = 300
Using FindLast() with String Values
The following example shows how to use FindLast() with string values −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Three");
list.AddLast("Four");
list.AddLast("Five");
Console.WriteLine("Elements in LinkedList...");
foreach (string res in list) {
Console.WriteLine(res);
}
LinkedListNode<string> val = list.FindLast("Five");
Console.WriteLine("Specified value = " + val.Value);
}
}
The output of the above code is −
Elements in LinkedList... One Two Three Four Five Specified value = Five
Handling Non-Existent Values
When searching for a value that doesn't exist, FindLast() returns null. Here's how to handle this scenario −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<int> list = new LinkedList<int>();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
LinkedListNode<int> found = list.FindLast(99);
if (found != null) {
Console.WriteLine("Found value: " + found.Value);
} else {
Console.WriteLine("Value 99 not found in the LinkedList");
}
LinkedListNode<int> existing = list.FindLast(20);
Console.WriteLine("Found existing value: " + existing.Value);
}
}
The output of the above code is −
Value 99 not found in the LinkedList Found existing value: 20
Conclusion
The FindLast() method is essential for locating the last occurrence of a specific value in a LinkedList. It returns the actual LinkedListNode<T> containing the value, or null if not found, making it useful for navigation and manipulation operations within the LinkedList structure.
