LinkedList Clear() Method in C#

CsharpProgrammingServer Side Programming

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...
raja
Published on 12-Sep-2018 12:56:52
Advertisements