Java LinkedList peek() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, peek() returns the head of the LinkedList, which is its first element, but leaves it in the list. If the list is empty it returns null and does not throw an exception.

The head means different things for different uses: the next person in a queue, or the top item of a stack that you filled with push. Either way peek lets you look before you decide to take it.

An instance method of java.util.LinkedList (since Java 1.5): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Queue, and LinkedList implements it. Call it on a LinkedList (or Queue) variable: a plain List variable cannot see it.

Syntax

Call it any time; an empty list is safe.

Java
LinkedList<String> list = new LinkedList<>();
String a = list.peek();        // null, the list is empty
list.add("X");
list.add("Y");
String b = list.peek();        // "X", and the list still has X and Y
CallWhat it does
list.peek()Returns the head, or null if the list is empty.
list.element()Returns the head, but throws if the list is empty.
list.poll()Returns the head and removes it.
list.peekLast()Looks at the tail instead.

peek never changes the list. Call it as often as you like.

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

Parameters

ParameterDescription
Nonepeek() takes no parameters.

Return Value

Return ValueDescription
EThe head of the list, or null if the list is empty.

Compare with Similar Methods

Methods that look at or take the head:

MethodHow it differs
element()Same as peek(), except it throws on an empty list.
poll()Takes the head out of the list; returns null if empty.
peekFirst()The Deque name for the same operation.
ArrayDeque.peek()The same call on an array-based deque.

Java Examples

Example 1 — Look at the Next Item Without Taking It

Peek at a queue of songs twice and show that nothing is removed.

Java
import java.util.LinkedList;

public class LinkedListPeekDemo {
    public static void main(String[] args) {
        LinkedList<String> songs = new LinkedList<>();
        songs.add("Blue Sky");
        songs.add("Night Drive");
        songs.add("Morning Light");

        System.out.println("peek: " + songs.peek());
        System.out.println("peek: " + songs.peek());
        System.out.println("songs: " + songs);
        System.out.println("size: " + songs.size());
    }
}

How It Works

  1. Three songs. "Blue Sky" is first, so it is the head.
  2. peek returns the head. The first call prints "Blue Sky".
  3. Peeking twice is harmless. The second call returns the same song, because nothing was removed.
  4. All songs remain. The list and the size 3 are exactly as before.

Example 2 — Peek Safely at an Empty List

Write a loop that keeps peeking until the list is empty, and show the null result at the end.

Java
import java.util.LinkedList;

public class LinkedListPeekEmpty {
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(10);
        stack.push(20);

        while (stack.peek() != null) {
            int top = stack.peek();
            System.out.println("top is " + top);
            stack.pop();
        }

        System.out.println("peek on empty: " + stack.peek());
    }
}

How It Works

  1. A stack of two numbers. push puts 20 on top of 10, so 20 is the head.
  2. The loop condition. peek() != null is true while there is still a head.
  3. Look, then pop. Each pass prints the top, 20 first and then 10, and pop() removes it.
  4. Empty at the end. When both are gone, peek() returns null and the loop stops with no exception.

Example 3 — Spot the Difference Between Empty and null

Store a null element on purpose and see why peek cannot tell it apart from an empty list.

Java
import java.util.LinkedList;

public class LinkedListPeekNull {
    public static void main(String[] args) {
        LinkedList<String> empty = new LinkedList<>();
        LinkedList<String> withNull = new LinkedList<>();
        withNull.add(null);

        System.out.println("empty.peek():    " + empty.peek());
        System.out.println("withNull.peek(): " + withNull.peek());
        System.out.println("empty.isEmpty():    " + empty.isEmpty());
        System.out.println("withNull.isEmpty(): " + withNull.isEmpty());
    }
}

How It Works

  1. Two different lists. One has no elements, and the other holds a single null.
  2. Same peek result. Both calls print null, so the result alone does not say which list is which.
  3. isEmpty tells them apart. The first list is empty (true), and the second is not (false).
  4. The lesson. Avoid storing null in a queue. If you must, test isEmpty() as well as the peek result.

Frequently Asked Questions

It returns the head (first element) without removing it. It returns null if the list is empty.
peek only looks at the head. poll returns the head and removes it. Both return null when the list is empty.
Use peek() after push(). push puts elements at the front, and peek reads the front.
Either the list is empty, or its first element is null. Use isEmpty() to check which one it is.
They return the same head. On an empty list peek returns null, while element throws NoSuchElementException.

Next: peekFirst()

Look at the first element without removing it; null if empty.

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

10 people found this page helpful