Back to Blog
Java

Java LinkedList removeFirst() Explained

Learn how to use Java's LinkedList.removeFirst() correctly: syntax, behavior on empty lists, O(1) performance, and comparisons with pollFirst() and ArrayDeque.

JavaLinkedListCollectionsData StructuresremoveFirst
Illustration of a Java LinkedList with the first node being removed by the removeFirst method, showing the updated head reference.

To remove and return the first element of a LinkedList, call removeFirst(). This article covers its syntax, behavior on an empty list, performance, and when another method such as pollFirst() may be more appropriate.

The removeFirst() Method in Java's LinkedList

The java.util.LinkedList class provides a removeFirst() method that removes and returns the first element of the list. This method is declared by the Deque interface, which LinkedList implements, and it is a direct way to access the head of the list. If you are working with a LinkedList and need to retrieve the first element while removing it, removeFirst() is the method to use.

Unlike remove(int index) or remove(Object o), removeFirst() operates on the head of the list in constant time, O(1), because it only updates the references of the first node. This makes it a natural fit for queue-like or stack-like operations where you frequently access the front of the collection.

Basic Usage and Syntax

Calling removeFirst() is straightforward. The method takes no arguments and returns the element that was removed. Here is a minimal example:

import java.util.LinkedList; LinkedList<String> tasks = new LinkedList<>(); tasks.add("compile"); tasks.add("test"); tasks.add("deploy"); String firstTask = tasks.removeFirst(); System.out.println(firstTask); // compile System.out.println(tasks); // [test, deploy]

The method modifies the list by removing the head node and shifting the head reference to the next node. The removed element is returned, allowing you to use it immediately. This behavior is identical to remove() from the Queue interface, but removeFirst() is explicit about which end of the list is affected.

What Happens When the List Is Empty

One of the most common pitfalls with removeFirst() is calling it on an empty list. The method throws NoSuchElementException if the list contains no elements. This is different from pollFirst(), which returns null when the list is empty. The choice between these two methods depends on how you want to handle the empty case.

import java.util.LinkedList; import java.util.NoSuchElementException; LinkedList<String> emptyList = new LinkedList<>(); try { emptyList.removeFirst(); } catch (NoSuchElementException e) { System.out.println("List is empty"); }

If your code expects the list to be non-empty at that point, removeFirst() is appropriate because it fails fast and alerts you to a logic error. If the absence of an element is a normal condition, use pollFirst() to avoid exception handling.

Performance Characteristics of removeFirst()

The LinkedList is a doubly-linked list, and removeFirst() operates on the head node. For a linked list, removing the head is a constant-time operation because it involves updating the first reference and the previous pointer of the new head. No shifting of elements occurs, unlike an ArrayList, where removing the first element requires moving all remaining elements to the left, resulting in O(n) time.

This makes removeFirst() a strong candidate when you frequently remove elements from the front of a collection. For example, implementing a queue with LinkedList and using removeFirst() (or poll()) gives you O(1) dequeue operations. The same applies to stack-like use if you add and remove at the head with addFirst() and removeFirst().

The constant-time behavior holds regardless of the list size. However, this advantage comes with a trade-off: LinkedList has higher memory overhead per element due to node objects and references, and random access is O(n). So the performance benefit of removeFirst() is most relevant when front removal is a dominant operation.

Comparing removeFirst() with remove() and pollFirst()

The LinkedList class offers several ways to remove the first element, and the differences matter for code clarity and exception handling.

MethodReturnsOn empty listInterface
removeFirst()Removed elementThrows NoSuchElementExceptionDeque
remove()Removed elementThrows NoSuchElementExceptionQueue
pollFirst()Removed element or nullReturns nullDeque
poll()Removed element or nullReturns nullQueue

remove() is equivalent to removeFirst() in behavior and is often used when you treat the LinkedList as a queue. pollFirst() is the safer variant when the list might be empty. The choice should be based on whether an empty list is an exceptional condition or a normal one.

For example, in a producer-consumer scenario where the consumer checks for work, using pollFirst() avoids exception overhead and lets you handle the null result gracefully. In contrast, if you are processing a batch that must have at least one item, removeFirst() will catch an invalid state early.

When LinkedList Is the Right Choice

removeFirst() is available on any Deque implementation, including LinkedList and ArrayDeque. If your primary need is to remove elements from the front, an ArrayDeque is often a better choice because it also provides O(1) removal from both ends and usually has lower memory overhead and better cache locality. LinkedList can be more useful when you need ListIterator operations that insert or remove elements at a known position, or when you need a doubly-linked list structure for other reasons.

Consider the following scenario: you are building a task scheduler that processes jobs in FIFO order. You could use LinkedList and call removeFirst() to dequeue. However, ArrayDeque would offer the same O(1) operation, often with less overhead. The decision comes down to whether you need the additional features of LinkedList, such as listIterator() for bidirectional traversal or the ability to insert at arbitrary positions.

If you are already using LinkedList because of its other capabilities, removeFirst() is the natural way to access the head. If you are only using it as a queue, consider switching to ArrayDeque for better performance in most cases.

Edge Cases and Common Mistakes

A common mistake is confusing removeFirst() with removeLast() or using it on a list that could be empty without handling the exception. Another subtle issue arises because LinkedList allows null elements. removeFirst() returns null if the first element is null, which is indistinguishable from an empty list when using pollFirst(). This can lead to ambiguity if you rely on null as a sentinel.

LinkedList<String> list = new LinkedList<>(); list.add(null); String value = list.pollFirst(); // returns null, but list was not empty

If your list can contain null, you cannot use pollFirst() to determine emptiness. In that case, check isEmpty() before calling removeFirst() or pollFirst() to avoid confusion.

Another edge case is using removeFirst() in a loop that processes all elements. If the loop drains the list, guard each call with !list.isEmpty(), otherwise you will get an exception on the last iteration. A safer pattern is to use pollFirst() in a while loop and break when it returns null.

LinkedList<Integer> numbers = new LinkedList<>(); // ... populate numbers Integer current; while ((current = numbers.pollFirst()) != null) { // process current }

This pattern works only if the list does not contain null values. If it does, you need a different approach, such as using an iterator or checking isEmpty() explicitly.

Finally, remember that removeFirst() is a mutating operation. If you need to keep the original list intact, make a copy or use peekFirst() to inspect the head without removing it. The peekFirst() method returns the first element but does not remove it, which is useful when you need to conditionally decide whether to remove the element.

Java LinkedList removeFirst(): Syntax, Empty List, and Performance | RYUSLOG DEV