Skip to content
Related Articles

Related Articles

LinkedList clear() Method in Java
  • Last Updated : 10 Dec, 2018

The Java.util.LinkedList.clear() method is used to remove all the elements from a linked list. Using the clear() method only clears all the element from the list and not deletes the list. In other words we can say that the clear() method is used to only empty an existing LinkedList.


void clear()

Parameters: This function does not accept any parameter.

Return Value: This method does not return any value.

Below program illustrate the Java.util.LinkedList.clear() method:

// Java code to illustrate boolean clear()
import java.util.LinkedList;
public class LinkedListDemo {
    public static void main(String args[])
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
        // Using add() method to add elements in the list
        // Displaying the List
        System.out.println("Original LinkedList:" + list);
        // Clearing the list
        // Accessing the List after clearing it
        System.out.println("List after clearing all elements: " + list);
        // Adding elements after clearing the list
        // Displaying the List
        System.out.println("After adding elements to empty list:" + list);


Original LinkedList:[Geeks, for, Geeks, 10, 20]
List after clearing all elements: []
After adding elements to empty list:[Geeks, for, Geeks]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :