Java LinkedList peekLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

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.

Java
Deque<String> deque = new LinkedList<>();
String x = deque.peekLast();      // null, empty
deque.addLast("A");
deque.addLast("B");
String y = deque.peekLast();      // "B"
CallWhat 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
// java.util.LinkedList
public E peekLast()

Parameters

ParameterDescription
NonepeekLast() takes no parameters.

Return Value

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

Compare with Similar Methods

Methods that look at the ends:

MethodHow it differs
getLast()Same element, but throws NoSuchElementException when empty.
peekFirst()Looks at the other end.
pollLast()Looks at the last element and also removes it.
ArrayDeque.peekLast()The same call on an array-based deque.

Java Examples

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.

Java
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);
    }
}

How It Works

  1. Scores are appended. The history grows at the tail, so newer scores come later in the list.
  2. peekLast shows the newest. It returns 160, the last element.
  3. peekFirst shows the oldest. It returns 120, the first element.
  4. 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.

Java
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());
    }
}

How It Works

  1. No exception on empty. peekLast() simply returns null when the list has no elements.
  2. One note. With a single element, the first and last element are the same, "buy stamps".
  3. A second note moves the tail. The tail is now "call Sam".
  4. 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.

Java
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());
    }
}

How It Works

  1. An empty trail. There is no last element yet.
  2. peekLast gives null. A plain == null check handles it, and no exception is needed.
  3. getLast throws. The exact same situation raises NoSuchElementException.
  4. With elements they agree. After adding "start" and "bridge", both methods return "bridge".

Frequently Asked Questions

It returns the last element without removing it, or null if the list is empty.
On an empty list peekLast returns null, and getLast throws NoSuchElementException.
No. Use pollLast() or removeLast() to remove it as well.
No. A Queue only exposes its head. peekLast comes from the Deque interface, which LinkedList also implements.
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

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.

12 people found this page helpful