Home Java LinkedList Methods peekLast() Java LinkedList peekLast() Method Definition In Java, peekLast() returns the last element (the tail) of the LinkedList and leaves it in the list. On an empty list it returns null.
The method is instant, because the list keeps a link to its tail. It is the tail-side partner of peekFirst(), and the safe alternative to getLast().
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<>();
String x = deque.peekLast(); // null, empty
deque.addLast("A");
deque.addLast("B");
String y = deque.peekLast(); // "B" Call What it does deque.peekLast()Returns the last element, or null if empty. deque.peekFirst()Returns the first element, or null if empty. deque.getLast()Returns the last element, but throws if empty. deque.pollLast()Returns the last element and removes it.
peekLast never changes the list.
Library declarations // java.util.LinkedList
public E peekLast() Parameters Parameter Description None peekLast() takes no parameters.
Return Value Return Value Description EThe last element, or null if the list is empty.
Compare with Similar Methods Methods that look at the ends:
Java Examples 1. Look at the back 2. Empty deque 3. Versus getLast Example 1 — Read the Newest Item in a List Peek at the newest score in a game history and the oldest score next to it.
import java.util.Deque;
import java.util.LinkedList;
public class LinkedListPeekLastDemo {
public static void main(String[] args) {
Deque<Integer> scores = new LinkedList<>();
scores.addLast(120);
scores.addLast(145);
scores.addLast(160);
System.out.println("newest score: " + scores.peekLast());
System.out.println("oldest score: " + scores.peekFirst());
System.out.println("history: " + scores);
}
} newest score: 160
oldest score: 120
history: [120, 145, 160] How It Works Scores are appended. The history grows at the tail, so newer scores come later in the list.peekLast shows the newest. It returns 160, the last element.peekFirst shows the oldest. It returns 120, the first element.No removal. The printed history still has all three scores.Example 2 — Peek at the End of an Empty Deque Show the null result on an empty deque, then add an element and peek again.
import java.util.LinkedList;
public class LinkedListPeekLastEmpty {
public static void main(String[] args) {
LinkedList<String> notes = new LinkedList<>();
System.out.println("empty: " + notes.peekLast());
notes.add("buy stamps");
System.out.println("one note: " + notes.peekLast());
notes.add("call Sam");
System.out.println("two notes: " + notes.peekLast());
System.out.println("size: " + notes.size());
}
} empty: null
one note: buy stamps
two notes: call Sam
size: 2 How It Works No exception on empty. peekLast() simply returns null when the list has no elements.One note. With a single element, the first and last element are the same, "buy stamps".A second note moves the tail. The tail is now "call Sam".Peeking never removes. After three peeks the size is 2, which is just the two adds.Example 3 — Pick Between peekLast and getLast Write a small check that decides what to do based on peekLast, and compare the result with getLast.
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListPeekLastCompare {
public static void main(String[] args) {
LinkedList<String> trail = new LinkedList<>();
if (trail.peekLast() == null) {
System.out.println("peekLast: trail is empty");
}
try {
trail.getLast();
} catch (NoSuchElementException e) {
System.out.println("getLast: NoSuchElementException");
}
trail.add("start");
trail.add("bridge");
System.out.println("peekLast: " + trail.peekLast());
System.out.println("getLast: " + trail.getLast());
}
} peekLast: trail is empty
getLast: NoSuchElementException
peekLast: bridge
getLast: bridge How It Works An empty trail. There is no last element yet.peekLast gives null. A plain == null check handles it, and no exception is needed.getLast throws. The exact same situation raises NoSuchElementException.With elements they agree. After adding "start" and "bridge", both methods return "bridge".Frequently Asked Questions What does LinkedList peekLast() do? It returns the last element without removing it, or null if the list is empty.
What is the difference between peekLast() and getLast()? On an empty list peekLast returns null, and getLast throws NoSuchElementException.
Does peekLast() remove the element? No. Use pollLast() or removeLast() to remove it as well.
Is there a Queue method for the last element? No. A Queue only exposes its head. peekLast comes from the Deque interface, which LinkedList also implements.
Can peekLast() return null for a non-empty list? Yes, when the last element is a stored null. Use isEmpty() to tell the two cases apart.
Next: poll() Remove and return the head, or null if the list is empty.
poll() tutorial → 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.
12 people found this page helpful
Helpful Share Copy link Suggestion