Clear a Linked List in C#



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)...
Updated on: 2020-06-23T08:05:42+05:30

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements