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


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

Updated on: 23-Jun-2020

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements