Last Updated : 11 Jul, 2025
In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty.
Syntax of LinkedList clear() Methodvoid clear()
Example: Here, we use the clear() method to remove elements from the LinkedList.
Java
// Java programm to demomstrate the
// working of clear() in LinkedList
import java.util.LinkedList;
public class GFG {
public static void main(String args[]) {
// Creating an empty LinkedList
LinkedList<String> l = new LinkedList<>();
// Using add() method to add
// elements in the LinkedList
l.add("Geeks");
l.add("for");
l.add("Geeks");
System.out.println("Original LinkedList: " + l);
// Clearing the LinkedList
l.clear();
// Accessing the LinkedList
// after clearing it
System.out.println(
"List after clearing all elements: " + l);
}
}
Original LinkedList: [Geeks, for, Geeks] List after clearing all elements: []
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4