Java LinkedList poll: How It Works
Understand how LinkedList.poll() works in Java: what it removes, what it returns on empty lists, and how it compares with remove(), peek(), and ArrayDeque.

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 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 declared in the Queue interface. LinkedList implements Deque, which extends Queue, so the method is available whether you use the list as a Queue, Deque, or directly as 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.
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 NoSuchElementExceptionThis 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 jobIn this pattern, offer() adds to the tail and poll() removes from the head. This is a simple in-memory queue loop. Because poll() returns null when the queue is empty, code can use the null result as a stop signal or fall back to another waiting mechanism. poll() itself does not block.
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 emptyIf you need to distinguish between an empty list and a null element, check isEmpty() after calling poll(): if poll() returned null and the list is empty, the queue was empty; if the list is not empty, the removed head was a null element. Alternatively, use peek() to inspect the head without removing it. Note that ArrayDeque, a common Queue replacement, does not allow null elements at all.
Performance Considerations for poll()
poll() on a LinkedList runs in O(1) time. It removes the first node and updates the head reference, so no shifting of elements occurs and no new node is allocated. 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 makes sense when you need the List interface, including indexed access, or when you need to combine queue and deque behavior with list operations.
| Operation | LinkedList | ArrayDeque |
|---|---|---|
| poll() | O(1) | O(1) |
| Memory per element | Higher | Lower |
| Random access | O(n) | Not supported |
| Middle insertion | O(n) to locate, O(1) once located | Not supported |
For most queue workloads, ArrayDeque wins. Use LinkedList when you need to combine queue semantics with list operations such as insertion or removal after you have already navigated to a position, 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 threadsIf 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, remember that poll() removes each element. Calling it once for every element originally in the list consumes the whole list; if the list does not contain null elements, the first null return appears only on an extra call after the list is already empty. Prefer a loop that checks emptiness: while (!list.isEmpty()) { Integer item = list.poll(); ... }, or while ((item = list.poll()) != null) only when you know the list cannot contain null elements.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.