Linked List – clear method in Java

The clear() method removes the elements present in the linked list. In this post, we will discuss the clear() method of the Linked List class in detail.

  • Method declaration – public void clear()
  • What does it do? It will remove all the elements present in the linked list, and the linked list will be empty. It can be checked using the isEmpty() method on the linked list.
  • What does it return? It won’t return anything to the calling function as its return type is void.
Code Example

We will take a linked list, add some objects, and then will use the clear() method on it.

import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>();

		linkedList.add("hi");
		linkedList.add("codekru");

		System.out.println("Before using clear method: " + linkedList);
		linkedList.clear();

		System.out.println("After using clear method: " + linkedList);

	}
}

Output –

Before using clear method: [hi, codekru]
After using clear method: []

Here, we can see that, after using the clear() method, the linked list was empty and can be started newly 😛

Internal implementation of the clear method

The clear method runs a loop on the linked list, marking the nodes as null, making them available for garbage collector in java.

    public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }
The time complexity of the clear() method

From the implementation of the clear() method shown above, we can see that the clear method runs a for loop over the linked list while marking the nodes as null. So, the average time complexity of the clear() method would be O(n) where n is the size of the linked list.

The What If scenarios

Q – What if we use the clear() method on a null linked list?

We will get a NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>();

		linkedList = null;

		linkedList.clear();

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.LinkedList.clear()" because "linkedList" is null

Please visit this link to learn more about the Linked List class and its methods in Java.

Well, this sums up about clear() method of the linked list. We hope that you have liked the article. If you have any doubts or concerns, please let us know in the comments, or you can mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.