C# Program to remove a node at the beginning of a Linked List

CsharpProgrammingServer Side Programming

To remove a node at the beginning of a LinkedList, use the RemoveFirst() method.

string [] employees = {"Peter","Robert","John","Jacob"};
LinkedList<string> list = new LinkedList<string>(employees);

Now, to remove the first element, use the RemoveFirst() method.

list.RemoveFirst();

Let us see the complete example.

Example

 Live Demo

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      string [] employees = {"Peter","Robert","John","Jacob"};
      LinkedList<string> list = new LinkedList<string>(employees);
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
      // removing first node
      list.RemoveFirst();
      Console.WriteLine("LinkedList after removing first node...");
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
   }
}

Output

Peter
Robert
John
Jacob
LinkedList after removing first node...
Robert
John
Jacob
raja
Published on 12-Sep-2018 16:16:03
Advertisements