C# Program to Find Node in Linked List



Firstly, create a new linked list −

LinkedList<string> myList = new LinkedList<string>();

Now add some elements in the linked list −

// 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");

Let’s now find a node and add a new node after that −

LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");

Example

You can try to run the following code to find a node in the linked list.

Live Demo

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");
      LinkedListNode<string> node = myList.Find("R");
      myList.AddAfter(node, "ADDED");
      foreach (var i in myList) {
         Console.WriteLine(i);
      }
   }
}

Output

P
Q
R
ADDED
S
T
U
Updated on: 2020-06-19T10:52:04+05:30

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements