C# program to remove the first occurrence of a node in a Linked List

CsharpProgrammingServer Side Programming

The following is our LinkedList list with nodes.

string [] students = {"Katie","Jennifer","Amy","Vera"};
LinkedList<string> list = new LinkedList<string>(students);

Now let us remove a node with the string element “Vera”.

For that, use Remove() method.

list.Remove("Vera");

Example

 Live Demo

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      string [] students = {"Katie","Jennifer","Amy","Vera"};
      LinkedList<string> list = new LinkedList<string>(students);
      foreach (var stu in list) {
         Console.WriteLine(stu);
      }
      // removing a node
      list.Remove("Vera");
      Console.WriteLine("LinkedList after removing a node...");
      foreach (var stu in list) {
         Console.WriteLine(stu);
      }
   }
}

Output

Katie
Jennifer
Amy
Vera
LinkedList after removing a node...
Katie
Jennifer
Amy
raja
Published on 12-Sep-2018 12:35:46
Advertisements