Back to Blog
Java

Java LinkedList poll: How It Works

java linkedlist poll: Learn how LinkedList.poll() works in Java, its return behavior on empty lists, and how it compares with remove() and other Queue methods.

JavaLinkedListQueueDequeCollectionspoll
Diagram of Java LinkedList poll removing the head element and returning it

When you work with a LinkedList in Java as a queue or deque, the poll() method is one of the first retrieval operations you reach for. It removes and returns the head of the list, or returns null if the list is empty. Understanding exactly what java linkedlist poll does—and what it does not do—prevents subtle bugs in code that processes elements in FIFO order.

What poll() Does on a LinkedList

The poll() method is inherited from the Queue interface. On a LinkedList, it removes the first element (the head) and returns it. If the list has no elements, it returns null instead of throwing an exception. This behavior is identical whether the LinkedList is referenced as a Queue, Deque, or directly as a LinkedList.

LinkedList<String> tasks = new LinkedList<>(); tasks.add("compile"); tasks.add("test"); tasks.add("deploy"); String next = tasks.poll(); // returns "compile", list now contains ["test", "deploy"]

After poll(), the head node is unlinked from the list, and the next node becomes the new head. The method runs in constant time, O(1), because it only updates the first node reference and the size counter.

poll() vs remove(): The Empty List Difference

remove() is the other common way to retrieve and remove the head of a LinkedList. The critical difference is how they behave on an empty list. poll() returns null, while remove() throws NoSuchElementException.

LinkedList<String> empty = new LinkedList<>(); String fromPoll = empty.poll(); // null String fromRemove = empty.remove(); // throws NoSuchElementException

This distinction matters when you are processing a stream of items that may legitimately be empty. Using poll() lets you check for null and decide whether to continue or stop without exception handling. If an empty state is an error condition in your logic, remove() makes that failure explicit.

Using poll() with LinkedList as a Queue

The most common use of poll() is in a FIFO queue. LinkedList implements Queue, so you can assign it to a Queue variable and call poll() without exposing the list-specific methods.

Queue<Job> jobQueue = new LinkedList<>(); jobQueue.offer(new Job("build")); jobQueue.offer(new Job("test")); Job current = jobQueue.poll(); // retrieves and removes the first job

In this pattern, offer() adds to the tail and poll() removes from the head. This is the standard producer–consumer loop for a simple in-memory queue. Because poll() returns null when the queue is empty, a worker thread can poll in a loop and idle when there is no work.

pollFirst() and pollLast() for Deque Operations

If you are using LinkedList as a Deque, you have access to pollFirst() and pollLast(). These behave like poll() but explicitly target either end of the list. pollFirst() is equivalent to poll() on a LinkedList; pollLast() removes and returns the tail.

Deque<String> deque = new LinkedList<>(); deque.addLast("left"); deque.addLast("right"); String first = deque.pollFirst(); // "left" String last = deque.pollLast(); // "right"

Both methods return null when the deque is empty. This is useful when you are implementing a double-ended queue and need to consume from either side without exception handling.

Null Handling and Empty List Edge Cases

Because poll() returns null on an empty list, you must be careful if your list can contain null elements. LinkedList allows null values, so a null return from poll() could mean either "empty" or "the head element was null." This ambiguity is rarely a problem in practice, but it is a reason to avoid storing null in a queue when you rely on poll() to signal emptiness.

LinkedList<String> list = new LinkedList<>(); list.add(null); String value = list.poll(); // returns null, but the list is now empty

If you need to distinguish between an empty list and a null element, check isEmpty() before calling poll(), or use peek() to inspect the head without removing it.

Performance Considerations for poll()

poll() on a LinkedList is O(1) in both time and memory allocation. It removes the first node and updates the head reference, so no shifting of elements occurs. This is in contrast to ArrayList, where removing the first element is O(n) because every subsequent element must be shifted left.

However, LinkedList has higher memory overhead per element than ArrayList because each node stores two references (next and previous) in addition to the data. If you are building a queue that will be polled frequently, ArrayDeque is often a better choice: it also offers O(1) poll() and uses a resizable array, which is more cache-friendly and consumes less memory per element. Use LinkedList when you also need to insert or remove elements in the middle, or when you need to iterate in both directions with ListIterator.

Choosing Between LinkedList and ArrayDeque for poll()

If your primary operation is poll() on a FIFO queue, ArrayDeque is generally preferable. It implements Deque, has O(1) poll() and offer(), and does not allocate a node per element. LinkedList only makes sense when you need list-specific operations like indexed access, or when you need to maintain insertion order while also supporting queue and deque behavior.

OperationLinkedListArrayDeque
poll()O(1)O(1)
Memory per elementHighLow
Random accessO(n)O(1)
Middle insertionO(1)O(n)

For most queue workloads, ArrayDeque wins. Use LinkedList when you need to combine queue semantics with frequent insertion or removal at arbitrary positions, or when you rely on the List interface.

Thread Safety and poll()

Neither LinkedList nor its poll() method is thread-safe. If multiple threads call poll() on the same instance concurrently, the internal structure can be corrupted. For a thread-safe queue, use ConcurrentLinkedQueue or a blocking queue like LinkedBlockingQueue. These classes provide atomic poll() operations that are safe for multi-threaded producers and consumers.

Queue<String> safeQueue = new ConcurrentLinkedQueue<>(); safeQueue.offer("job"); String item = safeQueue.poll(); // safe across threads

If you need to block when the queue is empty, LinkedBlockingQueue.take() is the appropriate method; poll() on a blocking queue returns null immediately when empty, just like LinkedList.

Common Mistakes with poll()

One frequent error is confusing poll() with peek(). peek() returns the head without removing it, while poll() removes it. Another mistake is assuming poll() throws an exception on an empty list; it does not. Developers coming from languages where removal on an empty collection is an error often forget to check for null.

LinkedList<Integer> numbers = new LinkedList<>(); Integer n = numbers.poll(); // null, not an exception if (n != null) { process(n); }

Also, when using poll() in a loop, remember that the list shrinks. If you iterate over the original size and call poll() that many times, you will get null on the last call because the list is already empty. Use while ((item = list.poll()) != null) instead.

java linkedlist poll: Practical Usage and Code Examples | RYUSLOG DEV