LinkedList RemoveFirst() Method in C#

CsharpProgrammingServer Side Programming

Let’s say the following is our LinkedList with integer nodes.

int [] num = {29, 40, 67, 89, 198, 234};
LinkedList<int> myList = new LinkedList<int>(num);

Now, if you want to remove the first element from the list, then use the RemoveFirst() method.

myList.RemoveFirst();

Example

 Live Demo

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {29, 40, 67, 89, 198, 234};
      LinkedList<int> myList = new LinkedList<int>(num);
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
      // removing first node
      myList.RemoveFirst();
      Console.WriteLine("LinkedList after removing the first node...");
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
   }
}

Output

29
40
67
89
198
234
LinkedList after removing the first node...
40
67
89
198
234
raja
Published on 12-Sep-2018 12:41:31
Advertisements