Java LinkedList removeFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, removeFirst() takes the first element (the head) out of the LinkedList and returns it. The second element becomes the new head.

It takes the same short time on a list of any size, since only the first link has to change. If the list is empty there is nothing to remove, so it throws NoSuchElementException.

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 Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.

Syntax

Call it on a list that is not empty.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
String gone = list.removeFirst();   // "A", list is now [B]
CallWhat it does
list.removeFirst()Removes and returns the head. Throws if empty.
list.pollFirst()Removes and returns the head, or null if empty.
list.remove()The queue name for the same action.
list.pop()The stack name for the same action.

removeFirst, remove() and pop() are three names for one operation.

Library declarations
Java
// java.util.LinkedList
public E removeFirst()

Parameters

ParameterDescription
NoneremoveFirst() takes no parameters.

Return Value

Return ValueDescription
EThe element that used to be first.

Compare with Similar Methods

Ways to take the first element:

MethodHow it differs
pollFirst()Returns null instead of throwing when the list is empty.
remove()The Queue name for the same operation.
getFirst()Reads the first element without removing it.
ArrayDeque.removeFirst()The array-based deque version.

Java Examples

Example 1 — Serve Customers from the Front of the List

Remove customers from the front one at a time and print who was served.

Java
import java.util.LinkedList;

public class LinkedListRemoveFirstDemo {
    public static void main(String[] args) {
        LinkedList<String> line = new LinkedList<>();
        line.add("Ruth");
        line.add("Sam");
        line.add("Tara");

        String served = line.removeFirst();
        System.out.println("served: " + served);
        System.out.println("waiting: " + line);

        served = line.removeFirst();
        System.out.println("served: " + served);
        System.out.println("waiting: " + line);
    }
}

How It Works

  1. Three people wait. Ruth arrived first, so she is at the front.
  2. First removal. removeFirst() returns "Ruth" and takes her out of the line.
  3. Second removal. Sam is now the head, so he is served next.
  4. Only Tara is left. The final waiting list holds a single name.

Example 2 — Empty a List with a Loop

Use removeFirst in a loop that stops when the list is empty, and add up the removed values.

Java
import java.util.LinkedList;

public class LinkedListRemoveFirstDrain {
    public static void main(String[] args) {
        LinkedList<Integer> pile = new LinkedList<>();
        for (int i = 1; i <= 4; i++) {
            pile.add(i * 5);
        }

        int total = 0;
        while (!pile.isEmpty()) {
            int value = pile.removeFirst();
            total += value;
            System.out.println("took " + value + ", left " + pile.size());
        }
        System.out.println("total: " + total);
    }
}

How It Works

  1. Four values. The pile holds 5, 10, 15 and 20.
  2. The guard. The loop only runs while isEmpty() is false, so removeFirst() never meets an empty list.
  3. Taken in order. Each pass takes the current head and shows how many items are left.
  4. Total of everything. The values add up to 50, and the pile ends empty.

Example 3 — See removeFirst Fail and pollFirst Return null

Call both methods on an empty list and compare the results.

Java
import java.util.LinkedList;
import java.util.NoSuchElementException;

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

        System.out.println("pollFirst: " + inbox.pollFirst());

        try {
            inbox.removeFirst();
        } catch (NoSuchElementException e) {
            System.out.println("removeFirst -> NoSuchElementException");
        }

        inbox.add("welcome mail");
        System.out.println("removeFirst: " + inbox.removeFirst());
        System.out.println("size now: " + inbox.size());
    }
}

How It Works

  1. An empty inbox. There is no first message.
  2. pollFirst returns null. It is the forgiving version, so the program just prints null.
  3. removeFirst throws. The strict version raises NoSuchElementException, caught and reported here.
  4. With a message it works. After one add, removeFirst() returns "welcome mail" and the size drops to 0.

Frequently Asked Questions

Call list.removeFirst(). It deletes the first element and returns it.
It throws a NoSuchElementException. Use pollFirst() to get null instead.
Both remove and return the first element. On an empty list removeFirst throws an exception and pollFirst returns null.
Yes, it takes constant time. Only the link to the first node is changed.
Yes. For a LinkedList, all three remove and return the first element and throw on an empty list.

Next: removeFirstOccurrence()

Remove the first element equal to a value, searching from the head.

removeFirstOccurrence() 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