Java LinkedList poll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

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.

Java
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
CallWhat 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
// java.util.LinkedList
public E poll()

Parameters

ParameterDescription
Nonepoll() takes no parameters.

Return Value

Return ValueDescription
EThe former head of the list, or null if the list was empty.

Compare with Similar Methods

Queue methods that take or look at the head:

MethodHow 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

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.

Java
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());
    }
}

How It Works

  1. Two messages. They wait in the order invoice, newsletter.
  2. poll takes the oldest. The first two calls return "invoice" and then "newsletter".
  3. An empty queue gives null. The third call has nothing left to remove, so it returns null with no exception.
  4. 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.

Java
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());
    }
}

How It Works

  1. Poll and test in one line. The assignment gets the next order, and != null checks whether there was one.
  2. Orders arrive in sequence. They are handled in the order 250, 80 and 120.
  3. A running total. The revenue grows to 250, then 330, then 450.
  4. 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.

Java
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);
    }
}

How It Works

  1. Three players in order. Kai is at the head of the queue.
  2. poll takes the next player. Each turn removes the head, so Kai plays first, then Lia, then Max.
  3. offer puts the player at the back. After playing, each player joins the end of the line, so the cycle repeats.
  4. Five turns, three players. Kai and Lia play a second time, and the queue ends as Max, Kai, Lia.

Frequently Asked Questions

It removes and returns the head (first element) of the list. If the list is empty it returns null.
Both remove the head. On an empty list poll returns null, and remove throws NoSuchElementException.
poll removes the head. peek only looks at it and leaves it in place.
Use while ((item = queue.poll()) != null) { ... }. The loop stops when the queue is empty.
Yes. poll comes from the Queue interface, and pollFirst comes from Deque. Both remove the first element or return null.

Next: pollFirst()

Remove and return the first element, or null if empty.

pollFirst() 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.

13 people found this page helpful