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


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements