Java LinkedList isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, isEmpty() returns true when the LinkedList contains no elements, and false otherwise. It is the same as asking whether size() == 0, but easier to read.

It runs in constant time. It is also the standard guard before methods that throw on an empty list, such as removeFirst(), pop() and getFirst().

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<>();
boolean a = list.isEmpty();   // true
list.add("A");
boolean b = list.isEmpty();   // false
CallWhat it does
list.isEmpty()true if the list has no elements.
list.size() == 0The same test, written less clearly.
list == nullA different test: is there a list object at all?
list.peek() == nullA way to check a queue that has no null elements.

isEmpty() tells you about the elements, not about the variable. A list that exists but holds nothing is empty. A variable that is null has no list at all.

Library declarations
Java
// java.util.LinkedList (inherited from AbstractCollection)
public boolean isEmpty()

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the list contains no elements.

Compare with Similar Methods

Ways to check for emptiness:

MethodHow it differs
size()Gives the exact count. size() == 0 is the same as isEmpty().
clear()Makes a list empty.
peek()Returns null on an empty queue; good for a quick check when no elements are null.
ArrayList.isEmpty()The same method on an array-backed list.

Java Examples

Example 1 — Check a List Before and After Adding

Check a new list, a list with one element, and a list after clear().

Java
import java.util.LinkedList;

public class LinkedListIsEmptyDemo {
    public static void main(String[] args) {
        LinkedList<String> basket = new LinkedList<>();
        System.out.println("new basket empty: " + basket.isEmpty());

        basket.add("apple");
        System.out.println("with apple empty: " + basket.isEmpty());

        basket.clear();
        System.out.println("after clear empty: " + basket.isEmpty());
        System.out.println("same as size 0: " + (basket.size() == 0));
    }
}

How It Works

  1. A new list is empty. There are no elements yet, so the answer is true.
  2. One element changes it. After adding "apple", isEmpty() returns false.
  3. clear empties it again. The answer goes back to true.
  4. It matches size() == 0. The two tests always agree, but isEmpty() reads more naturally.

Example 2 — Use isEmpty to Guard a While Loop

Serve all the people in a line with a loop that checks isEmpty each time.

Java
import java.util.LinkedList;

public class LinkedListIsEmptyGuard {
    public static void main(String[] args) {
        LinkedList<String> line = new LinkedList<>();
        line.add("Owen");
        line.add("Pia");

        int served = 0;
        while (!line.isEmpty()) {
            String person = line.removeFirst();
            served++;
            System.out.println("serving " + person);
        }

        System.out.println("served " + served + ", line empty: " + line.isEmpty());
    }
}

How It Works

  1. Two people in line. Owen is first and Pia is second.
  2. The loop test. The loop continues while the list is not empty, so removeFirst() is only called when there is a head.
  3. Each pass serves one person. Owen and then Pia are removed and counted.
  4. The loop ends on its own. After two passes the line is empty and the final line confirms it.

Example 3 — Tell an Empty List from a null Variable

Handle a list variable that may be null, and show that a list of nulls is not empty.

Java
import java.util.LinkedList;

public class LinkedListIsEmptyNull {
    static String describe(LinkedList<String> list) {
        if (list == null) {
            return "no list at all";
        }
        return list.isEmpty() ? "empty list" : "list with " + list.size() + " element(s)";
    }

    public static void main(String[] args) {
        LinkedList<String> none = null;
        LinkedList<String> empty = new LinkedList<>();
        LinkedList<String> nulls = new LinkedList<>();
        nulls.add(null);

        System.out.println("none:  " + describe(none));
        System.out.println("empty: " + describe(empty));
        System.out.println("nulls: " + describe(nulls));
    }
}

How It Works

  1. Three different situations. One variable holds no list, one holds an empty list, and one holds a list with a single null element.
  2. Check null first. Calling isEmpty() on a null variable would throw NullPointerException, so the helper tests list == null before anything else.
  3. An empty list is not null. The list exists and has no elements, so it is described as an empty list.
  4. A list of nulls is not empty. It contains one element, even though that element is null, so it is reported as having one element.

Frequently Asked Questions

Call list.isEmpty(). It returns true when the list has no elements.
There is no difference in result. isEmpty() is simply clearer to read.
It throws a NullPointerException. Check list == null first, or make sure the variable always refers to a list.
No. A list with a null element has size 1, so isEmpty() returns false.
Those methods throw NoSuchElementException on an empty list. A quick isEmpty() check avoids the exception.

Next: iterator()

Walk the list from the first element to the last, one at a time.

iterator() 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.

12 people found this page helpful