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

CsharpProgrammingServer Side Programming

The following is our LinkedList.

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

Now, let’s say you need to remove the last node i.e. “Jamie”. For that, use the RemoveLast() method.

list.RemoveLast();

Example

 Live Demo

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

Output

Patrick
Robert
John
Jacob
Jamie
LinkedList after removing last node...
Patrick
Robert
John
Jacob
raja
Published on 12-Sep-2018 12:50:13
Advertisements