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


To find the last 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<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);
   }
}

Output

This will produce the following output −

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

Example

Let us see another example −

 Live Demo

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

Output

This will produce the following output −

Elements in LinkedList...
One
Two
Three
Four
Five
Specified value = Five

Updated on: 11-Dec-2019

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements