Removing first occurrence of specified value from LinkedList in C#


To remove the first occurrence of specified value from LinkedList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("A");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("A");
      list.AddLast("H");
      list.AddLast("A");
      list.AddLast("j");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
      LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      list.Remove("A");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
      demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

Output

This will produce the following output −

Count of nodes = 10
Elements in LinkedList... (Enumerator iterating through LinkedList)
A
B
C
A
E
F
A
H
A
j
Count of nodes = 9
Elements in LinkedList... (Enumerator iterating through LinkedList)
B
C
A
E
F
A
H
A
j

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("Three");
      list.AddLast("Three");
      list.AddLast("Four");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
      LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      list.Remove("Three");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
      demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

Output

This will produce the following output −

Count of nodes = 6
Elements in LinkedList... (Enumerator iterating through LinkedList)
One
Two
Three
Three
Three
Four
Count of nodes = 5
Elements in LinkedList... (Enumerator iterating through LinkedList)
One
Two
Three
Three
Four

Updated on: 06-Dec-2019

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements