Removing the specified node from the LinkedList in C#?

CsharpServer Side ProgrammingProgramming

To remove the specified node from the LinkedList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      list.AddLast(400);
      list.AddLast(500);
      list.AddLast(300);
      list.AddLast(500);
      Console.WriteLine("LinkedList elements...");
      foreach(int i in list) {
         Console.WriteLine(i);
      }
      LinkedListNode<int> val = list.FindLast(300);
      Console.WriteLine("Specified value = "+val.Value);
      list.Remove(500);
      Console.WriteLine("LinkedList elements...UPDATED");
      foreach(int i in list) {
         Console.WriteLine(i);
      }
   }
}

Output

This will produce the following output −

LinkedList elements...
100
200
300
400
500
300
500
Specified value = 300
LinkedList elements...UPDATED
100
200
300
400
300
500

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("Mark");
      list.AddLast("David");
      list.AddLast("Harry");
      list.AddLast("John");
      list.AddLast("Kevin");
      string[] strArr = new string[5];
      list.CopyTo(strArr, 0);
      Console.WriteLine("LinkedList elements...after copying to array");
      foreach(string str in strArr) {
         Console.WriteLine(str);
      }
      list.Remove("Harry");
      Console.WriteLine("LinkedList elements...UPDATED");
      foreach(string str in list) {
         Console.WriteLine(str);
      }
   }
}

Output

This will produce the following output −

LinkedList elements...after copying to array
Mark
David
Harry
John
Kevin
LinkedList elements...UPDATED
Mark
David
John
Kevin
raja
Published on 16-Dec-2019 09:33:55
Advertisements