Remove First Occurrence of a Node in a Linked List - C# Program



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: 2020-06-23T07:45:07+05:30

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements