Sử dụng phương thức Clear () để xóa LinkedList. Thao tác này sẽ xóa tất cả các nút khỏi LinkedList.
Giả sử sau đây là LinkedList của chúng tôi -
int [] num = {30, 65, 80, 95, 110, 135};
LinkedList<int> list = new LinkedList<int>(num); Để xóa Danh sách liên kết.
list.Clear();
Ví dụ
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);
}
}
} Đầu ra
30 65 80 95 110 135 No node in the LinkedList now...