Home Java LinkedList Methods pollFirst() Java LinkedList pollFirst() Method 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.
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 Call What 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.util.LinkedList
public E pollFirst() Parameters Parameter Description None pollFirst() takes no parameters.
Return Value Return Value Description EThe former first element, or null if the list was empty.
Important
When the list may contain null elements, a null result is ambiguous. In algorithms that poll from both ends, check the size before each pair of polls, so that you never rely on null to mean "empty".
Compare with Similar Methods Ways to take the first element:
Java Examples 1. Take from the front 2. Palindrome check 3. Drop old values Example 1 — Take Elements from the Front of a Deque Poll a deque three times, where the third call finds it empty.
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);
}
} took: soup
took: salad
took: null
tray: [] How It Works Two dishes on the tray. "soup" is at the front and "salad" behind it.First two polls. Each one removes the front dish and returns it, soup and then salad.Third poll. The tray is empty, so pollFirst() returns null rather than throwing.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.
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));
}
}
} level -> true
radar -> true
hello -> false
noon -> true
a -> true How It Works Letters in a deque. Each character of the word is appended, so the deque keeps the original order.Compare the two ends. pollFirst() takes the left letter and pollLast() takes the right letter, and they must match.Work toward the middle. The loop stops when one or no letters are left, because a single middle letter always matches itself.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.
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);
}
} expired: 100
expired: 130
recent hits: [170, 205] How It Works Times in ascending order. The oldest hit is at the front, so expired ones are found first.A time limit of 60. At time 210, a hit is too old when it happened more than 60 units earlier.peekFirst decides, pollFirst removes. 100 and 130 are older than that limit, so they are removed and reported.The loop stops at 170. It is only 40 units old, so it stays, together with 205.Frequently Asked Questions What does LinkedList pollFirst() do? It removes and returns the first element of the list, or returns null if the list is empty.
What is the difference between pollFirst() and removeFirst()? On an empty list pollFirst returns null, and removeFirst throws NoSuchElementException.
Is pollFirst() the same as poll()? Yes. poll comes from the Queue interface and pollFirst from the Deque interface. Both remove the first element.
How can I use pollFirst() and pollLast() together? 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.
Does pollFirst() change the list? Yes, it removes the element. Use peekFirst() to only look at it.
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.
14 people found this page helpful
Helpful Share Copy link Suggestion