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
Call
What 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
Parameter
Description
None
pollLast() takes no parameters.
Return Value
Return Value
Description
E
The former last element, or null if the list was empty.