Find the first node in LinkedList containing the specified value in C#


To find the first node in LinkedList containing the specified value, the code is as follows −

Example

 Live Demo

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);
   }
}

Output

This will produce the following output −

LinkedList elements...
John
Tim
Kevin
Jacob
Emma
Ryan
Brad
Carl
Specified value = Jacob

Example

Let us now see another example −

 Live Demo

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);
   }
}

Output

This will produce the following output −

LinkedList elements...
100
200
300
400
500
Specified value = 300

Updated on: 10-Dec-2019

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements