LinkedList Clear() Method in C#


Use the Clear() method to clear a LinkedList. This will remove all the nodes from the LinkedList.

Let’s say the following is our LinkedList −

int [] num = {30, 65, 80, 95, 110, 135};
LinkedList<int> list = new LinkedList<int>(num);

To clear the LinkedList.

list.Clear();

Example

 Live Demo

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {30, 65, 80, 95, 110, 135};
      LinkedList<int> list = new LinkedList<int>(num);
      foreach (var n in list) {
         Console.WriteLine(n);
      }
      // clear
      list.Clear();
      Console.WriteLine("No node in the LinkedList now...");
      foreach (var n in list) {
         Console.WriteLine(n);
      }
   }
}

Output

30
65
80
95
110
135
No node in the LinkedList now...

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements