Remove Node at the End of a Linked List in C#



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
Updated on: 2020-06-23T07:52:28+05:30

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements