Clear a Linked List in C#

CsharpProgrammingServer Side Programming

Clear a LinkedList using Clear() method.

Let us first set a LinkedList.

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

Now, let us clear the LinkedList.

list.Clear();

Let us see the complete code.

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);
      }
      // clearing list
      list.Clear();
      Console.WriteLine("LinkedList after removing the nodes (empty list)...");
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
   }
}

Output

Patrick
Robert
John
Jacob
Jamie
LinkedList after removing the nodes (empty list)...
raja
Published on 12-Sep-2018 16:38:14
Advertisements