Home Java LinkedList Methods poll() Java LinkedList poll() Method Definition In Java, poll() removes the head of the LinkedList and returns it. If the list is empty it returns null instead of throwing an exception.
It is the standard "take the next item" method of the Queue interface. Because an empty queue is a normal situation, a loop such as while ((item = queue.poll()) != null) is a common way to process everything.
An instance method of java.util.LinkedList (since Java 1.5): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Queue, and LinkedList implements it. Call it on a LinkedList (or Queue) variable: a plain List variable cannot see it.
Syntax Call it on a queue; an empty one is safe.
Queue<String> queue = new LinkedList<>();
queue.offer("A");
queue.offer("B");
String x = queue.poll(); // "A"
String y = queue.poll(); // "B"
String z = queue.poll(); // null, the queue is empty Call What it does queue.poll()Removes and returns the head, or null if empty. queue.remove()Removes and returns the head, but throws if empty. queue.peek()Returns the head without removing it. queue.offer(e)Adds to the tail; the opposite end.
poll changes the list. Use peek() when you only want to look.
Library declarations // java.util.LinkedList
public E poll() Parameters Parameter Description None poll() takes no parameters.
Return Value Return Value Description EThe former head of the list, or null if the list was empty.
Important
A LinkedList may contain null elements, and then a null from poll() is ambiguous. When you use the list as a queue, never add null. The processing loop while ((x = queue.poll()) != null) would stop early on a stored null.
Compare with Similar Methods Queue methods that take or look at the head:
Method How it differs remove()Same removal, but throws NoSuchElementException when empty. peek()Looks at the head without removing it. pollFirst()The Deque name for the same operation. ArrayDeque.poll()The same call on an array-based deque.
Java Examples 1. Take the next item 2. Process everything 3. Round robin Example 1 — Take Items Off a Queue with poll Poll a queue of messages one at a time, including one extra poll after it is empty.
import java.util.LinkedList;
import java.util.Queue;
public class LinkedListPollDemo {
public static void main(String[] args) {
Queue<String> inbox = new LinkedList<>();
inbox.offer("invoice");
inbox.offer("newsletter");
System.out.println("1st: " + inbox.poll());
System.out.println("2nd: " + inbox.poll());
System.out.println("3rd: " + inbox.poll());
System.out.println("size: " + inbox.size());
}
} 1st: invoice
2nd: newsletter
3rd: null
size: 0 How It Works Two messages. They wait in the order invoice, newsletter.poll takes the oldest. The first two calls return "invoice" and then "newsletter".An empty queue gives null. The third call has nothing left to remove, so it returns null with no exception.Nothing remains. The size is 0.Example 2 — Process a Queue in a poll Loop Use the classic while loop that stops when poll returns null.
import java.util.LinkedList;
import java.util.Queue;
public class LinkedListPollLoop {
public static void main(String[] args) {
Queue<Integer> orders = new LinkedList<>();
orders.offer(250);
orders.offer(80);
orders.offer(120);
int revenue = 0;
Integer order;
while ((order = orders.poll()) != null) {
revenue += order;
System.out.println("processed " + order + ", revenue " + revenue);
}
System.out.println("queue empty: " + orders.isEmpty());
}
} processed 250, revenue 250
processed 80, revenue 330
processed 120, revenue 450
queue empty: true How It Works Poll and test in one line. The assignment gets the next order, and != null checks whether there was one.Orders arrive in sequence. They are handled in the order 250, 80 and 120.A running total. The revenue grows to 250, then 330, then 450.The loop ends by itself. When poll returns null the loop stops, and the queue is empty.Example 3 — Give Each Player a Turn in Round-Robin Order Poll the next player, let them play, and offer them back to the end of the queue.
import java.util.LinkedList;
import java.util.Queue;
public class LinkedListPollRound {
public static void main(String[] args) {
Queue<String> players = new LinkedList<>();
players.offer("Kai");
players.offer("Lia");
players.offer("Max");
for (int turn = 1; turn <= 5; turn++) {
String current = players.poll();
System.out.println("turn " + turn + ": " + current);
players.offer(current);
}
System.out.println("order now: " + players);
}
} turn 1: Kai
turn 2: Lia
turn 3: Max
turn 4: Kai
turn 5: Lia
order now: [Max, Kai, Lia] How It Works Three players in order. Kai is at the head of the queue.poll takes the next player. Each turn removes the head, so Kai plays first, then Lia, then Max.offer puts the player at the back. After playing, each player joins the end of the line, so the cycle repeats.Five turns, three players. Kai and Lia play a second time, and the queue ends as Max, Kai, Lia.Frequently Asked Questions What does LinkedList poll() do? It removes and returns the head (first element) of the list. If the list is empty it returns null.
What is the difference between poll() and remove()? Both remove the head. On an empty list poll returns null, and remove throws NoSuchElementException.
What is the difference between poll() and peek()? poll removes the head. peek only looks at it and leaves it in place.
How do I process all items of a queue? Use while ((item = queue.poll()) != null) { ... }. The loop stops when the queue is empty.
Is poll() the same as pollFirst()? Yes. poll comes from the Queue interface, and pollFirst comes from Deque. Both remove the first element or return null.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion