Java LinkedList clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, clear() removes every element from the LinkedList. Afterwards size() is 0 and isEmpty() is true.

The list object itself stays alive and can be filled again. The method returns nothing, and it works in time proportional to the number of elements, because each link is cut so that the elements can be garbage collected.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Call it with no arguments.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.clear();                    // []
boolean empty = list.isEmpty();  // true
CallWhat it does
list.clear()Removes every element. Returns nothing.
list = new LinkedList<>()Points the variable at a brand-new empty list instead.
list.isEmpty()Checks whether the list has no elements.
list.removeAll(c)Removes only selected elements.

clear() keeps the same list object. Any other variable that points to the list sees it become empty too.

Library declarations
Java
// java.util.LinkedList
public void clear()

Parameters

ParameterDescription
Noneclear() takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned.

Compare with Similar Methods

Ways to empty or reset a list:

MethodHow it differs
removeAll(c)Removes selected elements only.
isEmpty()Checks for an empty list, for example after clear().
subList(from, to).clear()Removes only a range of the list.
ArrayList.clear()The same method on an array-backed list; it keeps its internal array.

Java Examples

Example 1 — Clear a List and Check Its State

Fill a list, clear it, and print its size and emptiness.

Java
import java.util.LinkedList;

public class LinkedListClearDemo {
    public static void main(String[] args) {
        LinkedList<String> cart = new LinkedList<>();
        cart.add("tea");
        cart.add("honey");
        cart.add("cups");
        System.out.println("before: " + cart + " size " + cart.size());

        cart.clear();

        System.out.println("after:  " + cart + " size " + cart.size());
        System.out.println("is empty: " + cart.isEmpty());
    }
}

How It Works

  1. A cart with three items. The size is 3.
  2. clear removes them all. One call is enough, and no loop is needed.
  3. An empty list. It prints as empty brackets, and the size is 0.
  4. isEmpty confirms. It returns true, because no elements remain.

Example 2 — Reuse One List for Several Rounds

Clear a list between rounds of a loop instead of creating a new list each time.

Java
import java.util.LinkedList;

public class LinkedListClearReuse {
    public static void main(String[] args) {
        LinkedList<Integer> batch = new LinkedList<>();

        for (int round = 1; round <= 3; round++) {
            batch.clear();
            for (int i = 1; i <= round; i++) {
                batch.add(round * 10 + i);
            }
            System.out.println("round " + round + ": " + batch);
        }
    }
}

How It Works

  1. One list for all rounds. The same batch object is used every time.
  2. Cleared at the start. Each round begins with clear(), so values from the previous round never leak in.
  3. Growing batches. Round 1 adds one number, round 2 adds two, and round 3 adds three.
  4. Only the latest values appear. Every printed line shows just the numbers of that round.

Example 3 — See Who Else Sees the Cleared List

Clear a list that has an alias and a separate copy, and compare what each one shows.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListClearAlias {
    public static void main(String[] args) {
        LinkedList<String> original = new LinkedList<>(Arrays.asList("x", "y", "z"));
        LinkedList<String> alias = original;
        LinkedList<String> copy = new LinkedList<>(original);

        original.clear();

        System.out.println("original: " + original);
        System.out.println("alias:    " + alias);
        System.out.println("copy:     " + copy);
        System.out.println("alias is same object: " + (alias == original));
    }
}

How It Works

  1. Three variables. alias points to the very same list object, and copy is a new list with the same elements.
  2. clear empties the shared object. The original list loses all its elements.
  3. The alias sees it too. It is not a separate list, so it also prints as empty.
  4. The copy is safe. It has its own nodes, so x, y and z are still there, and the last line confirms that the alias is the same object.

Frequently Asked Questions

Call list.clear(). It removes all elements and leaves an empty list that can be used again.
clear() empties the existing object, so every variable that points to it sees an empty list. new LinkedList<>() creates a separate list and leaves the old one untouched.
No. Its return type is void.
No. Copy the list first with new LinkedList<>(list) if you may need them later.
Yes. It simply does nothing and throws no exception.

Next: clone()

Make a shallow copy of the list.

clone() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful