Java LinkedList pollFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, pollFirst() removes the first element of the LinkedList and returns it, or returns null when the list is empty.

It is the same as poll(), with a name that fits the Deque idea of two ends. Together with pollLast() it lets you eat the list from both sides, which is useful for checks such as palindromes.

An instance method of java.util.LinkedList (since Java 1.6): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.

Syntax

Use it on a Deque or LinkedList variable.

Java
Deque<String> deque = new LinkedList<>();
deque.addLast("A");
deque.addLast("B");
String a = deque.pollFirst();   // "A"
String b = deque.pollFirst();   // "B"
String c = deque.pollFirst();   // null, empty
CallWhat it does
deque.pollFirst()Removes and returns the first element, or null.
deque.pollLast()Removes and returns the last element, or null.
deque.removeFirst()Same removal, but throws if empty.
deque.peekFirst()Looks at the first element without removing it.

pollFirst changes the list. Use peekFirst() to look without removing.

Library declarations
Java
// java.util.LinkedList
public E pollFirst()

Parameters

ParameterDescription
NonepollFirst() takes no parameters.

Return Value

Return ValueDescription
EThe former first element, or null if the list was empty.

Compare with Similar Methods

Ways to take the first element:

MethodHow it differs
poll()The Queue name for the same operation.
removeFirst()Same removal, but throws NoSuchElementException when empty.
pollLast()The same operation at the tail.
ArrayDeque.pollFirst()The array-based deque version.

Java Examples

Example 1 — Take Elements from the Front of a Deque

Poll a deque three times, where the third call finds it empty.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListPollFirstDemo {
    public static void main(String[] args) {
        Deque<String> tray = new LinkedList<>();
        tray.addLast("soup");
        tray.addLast("salad");

        System.out.println("took: " + tray.pollFirst());
        System.out.println("took: " + tray.pollFirst());
        System.out.println("took: " + tray.pollFirst());
        System.out.println("tray: " + tray);
    }
}

How It Works

  1. Two dishes on the tray. "soup" is at the front and "salad" behind it.
  2. First two polls. Each one removes the front dish and returns it, soup and then salad.
  3. Third poll. The tray is empty, so pollFirst() returns null rather than throwing.
  4. An empty result. The tray prints as an empty pair of brackets.

Example 2 — Check a Palindrome from Both Ends

Load the letters of a word into a deque, then compare the first and last letters until none are left.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListPollFirstPalindrome {
    static boolean isPalindrome(String word) {
        Deque<Character> letters = new LinkedList<>();
        for (char c : word.toCharArray()) {
            letters.addLast(c);
        }
        while (letters.size() > 1) {
            if (!letters.pollFirst().equals(letters.pollLast())) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String[] words = {"level", "radar", "hello", "noon", "a"};
        for (String w : words) {
            System.out.println(w + " -> " + isPalindrome(w));
        }
    }
}

How It Works

  1. Letters in a deque. Each character of the word is appended, so the deque keeps the original order.
  2. Compare the two ends. pollFirst() takes the left letter and pollLast() takes the right letter, and they must match.
  3. Work toward the middle. The loop stops when one or no letters are left, because a single middle letter always matches itself.
  4. Results. "level", "radar", "noon" and "a" read the same both ways, but "hello" fails at the first pair, h and o.

Example 3 — Remove Values from the Front Until a Limit

Drop expired timestamps from the front of a list until the first one is recent enough.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListPollFirstExpire {
    public static void main(String[] args) {
        Deque<Integer> hits = new LinkedList<>();
        hits.add(100);
        hits.add(130);
        hits.add(170);
        hits.add(205);
        int now = 210;

        while (hits.peekFirst() != null && now - hits.peekFirst() > 60) {
            System.out.println("expired: " + hits.pollFirst());
        }
        System.out.println("recent hits: " + hits);
    }
}

How It Works

  1. Times in ascending order. The oldest hit is at the front, so expired ones are found first.
  2. A time limit of 60. At time 210, a hit is too old when it happened more than 60 units earlier.
  3. peekFirst decides, pollFirst removes. 100 and 130 are older than that limit, so they are removed and reported.
  4. The loop stops at 170. It is only 40 units old, so it stays, together with 205.

Frequently Asked Questions

It removes and returns the first element of the list, or returns null if the list is empty.
On an empty list pollFirst returns null, and removeFirst throws NoSuchElementException.
Yes. poll comes from the Queue interface and pollFirst from the Deque interface. Both remove the first element.
Take one element from each end in a loop. It is a neat way to test palindromes or to pair up the smallest and largest items of a sorted list.
Yes, it removes the element. Use peekFirst() to only look at it.

Next: pollLast()

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

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

14 people found this page helpful