Java LinkedList pollLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, pollLast() removes the last element of the LinkedList and returns it, or returns null if the list is empty.

It is the tail-side partner of pollFirst(), and the quiet alternative to removeLast(). Since the list is doubly linked, removing from the tail is as quick as removing from the head.

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 x = deque.pollLast();   // "B"
String y = deque.pollLast();   // "A"
String z = deque.pollLast();   // null, empty
CallWhat it does
deque.pollLast()Removes and returns the last element, or null.
deque.pollFirst()Removes and returns the first element, or null.
deque.removeLast()Same removal, but throws if empty.
deque.peekLast()Looks at the last element without removing it.

pollLast changes the list. Use peekLast() to look without removing.

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

Parameters

ParameterDescription
NonepollLast() takes no parameters.

Return Value

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

Compare with Similar Methods

Ways to take the last element:

MethodHow it differs
removeLast()Same removal, but throws NoSuchElementException when empty.
pollFirst()The same operation at the head.
peekLast()Looks at the last element without removing it.
ArrayDeque.pollLast()The array-based deque version.

Java Examples

Example 1 — Take Elements from the Back of a Deque

Poll a deque from the back until it is empty and see the null at the end.

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

public class LinkedListPollLastDemo {
    public static void main(String[] args) {
        Deque<Integer> numbers = new LinkedList<>();
        numbers.add(3);
        numbers.add(6);
        numbers.add(9);

        System.out.println("1: " + numbers.pollLast());
        System.out.println("2: " + numbers.pollLast());
        System.out.println("3: " + numbers.pollLast());
        System.out.println("4: " + numbers.pollLast());
    }
}

How It Works

  1. Three numbers in order. The list holds 3, 6 and 9, and 9 is at the back.
  2. Polling from the back. The first three calls return 9, 6 and 3, in the reverse of the order they were added.
  3. The list is now empty. All three numbers have been removed.
  4. The fourth call returns null. There is nothing left, and no exception is thrown.

Example 2 — Remove Empty Strings from the End of a Row

Cut off empty cells at the end of a spreadsheet row with peekLast and pollLast.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListPollLastTrim {
    public static void main(String[] args) {
        LinkedList<String> row = new LinkedList<>(
            Arrays.asList("id", "", "name", "", "", ""));
        System.out.println("before: " + row + " size " + row.size());

        while ("".equals(row.peekLast())) {
            row.pollLast();
        }

        System.out.println("after:  " + row + " size " + row.size());
    }
}

How It Works

  1. A row with blanks. The row has six cells, and the last three are empty strings.
  2. peekLast checks first. "".equals(...) is true while the last cell is blank, and false once it is "name".
  3. pollLast trims. Each pass removes one blank cell from the end, so three are dropped.
  4. The middle blank stays. Only trailing blanks are removed, so the empty cell between "id" and "name" remains and the size is 3.

Example 3 — Keep a Short Recent-Items List

Add each new file at the front, and drop the oldest from the back when the list has more than three.

Java
import java.util.LinkedList;

public class LinkedListPollLastRecent {
    public static void main(String[] args) {
        LinkedList<String> recent = new LinkedList<>();
        String[] opened = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt"};

        for (String file : opened) {
            recent.addFirst(file);
            if (recent.size() > 3) {
                String dropped = recent.pollLast();
                System.out.println("dropped " + dropped);
            }
        }
        System.out.println("recent: " + recent);
    }
}

How It Works

  1. Newest at the front. Every opened file goes to the head of the list.
  2. Three files fit. Nothing is dropped for a.txt, b.txt or c.txt.
  3. The oldest is at the back. When d.txt arrives, pollLast() removes a.txt, and e.txt then pushes out b.txt.
  4. The result. The list keeps the three newest files, with e.txt first.

Frequently Asked Questions

It removes and returns the last element of the list, or returns null if the list is empty.
On an empty list pollLast returns null, and removeLast throws NoSuchElementException.
Yes. The list keeps a link to its last node, so the removal takes constant time.
No. A Queue only works at its head. pollLast belongs to the Deque interface.
Yes, it removes the element. Use peekLast() to only look at it.

Next: pop()

Pop the top element of the stack, or throw if it is empty.

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

15 people found this page helpful