Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Clear a Linked List in C#
The Clear() method in C# is used to remove all nodes from a LinkedList<T>. This method provides an efficient way to empty the entire linked list in a single operation, resetting the Count property to zero.
Syntax
Following is the syntax for the Clear() −
linkedList.Clear();
Parameters
The Clear() method takes no parameters.
Return Value
The Clear() method does not return any value. It is a void method that modifies the linked list in-place.
Using Clear() Method
Example
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);
Console.WriteLine("Original LinkedList:");
foreach (var emp in list) {
Console.WriteLine(emp);
}
Console.WriteLine("Count before Clear(): " + list.Count);
// clearing list
list.Clear();
Console.WriteLine("Count after Clear(): " + list.Count);
Console.WriteLine("LinkedList after clearing (empty list):");
foreach (var emp in list) {
Console.WriteLine(emp);
}
Console.WriteLine("LinkedList is now empty.");
}
}
The output of the above code is −
Original LinkedList: Patrick Robert John Jacob Jamie Count before Clear(): 5 Count after Clear(): 0 LinkedList after clearing (empty list): LinkedList is now empty.
Clear() vs Other Removal Methods
| Method | Purpose | Time Complexity |
|---|---|---|
Clear() |
Removes all nodes from the LinkedList | O(n) |
RemoveFirst() |
Removes only the first node | O(1) |
RemoveLast() |
Removes only the last node | O(1) |
Remove(value) |
Removes the first occurrence of a specific value | O(n) |
Practical Use Case
Example
using System;
using System.Collections.Generic;
class TaskManager {
static void Main() {
LinkedList<string> tasks = new LinkedList<string>();
// Add some tasks
tasks.AddLast("Complete project report");
tasks.AddLast("Review code changes");
tasks.AddLast("Attend team meeting");
Console.WriteLine("Current tasks:");
foreach (var task in tasks) {
Console.WriteLine("- " + task);
}
// Clear all tasks at end of day
Console.WriteLine("\nEnd of day - clearing all tasks...");
tasks.Clear();
Console.WriteLine("Tasks remaining: " + tasks.Count);
Console.WriteLine("Ready for new tasks tomorrow!");
}
}
The output of the above code is −
Current tasks: - Complete project report - Review code changes - Attend team meeting End of day - clearing all tasks... Tasks remaining: 0 Ready for new tasks tomorrow!
Conclusion
The Clear() method provides an efficient way to remove all elements from a LinkedList<T> in one operation. It's particularly useful when you need to reset the list while keeping the same LinkedList instance for reuse, making it ideal for scenarios like clearing temporary data or resetting collections.
