Java LinkedList pop() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, pop() removes the first element of the LinkedList and returns it. When you build a stack with push, the first element is the top, so pop takes off the item that was pushed last.

That order is called LIFO: last in, first out. If the stack is empty there is nothing to pop, so the method throws NoSuchElementException.

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 with push.

Java
LinkedList<String> stack = new LinkedList<>();
stack.push("A");
stack.push("B");
String top = stack.pop();   // "B"
String next = stack.pop();  // "A"
CallWhat it does
stack.pop()Removes and returns the top. Throws if empty.
stack.push(e)Puts an element on the top.
stack.peek()Looks at the top without removing it.
stack.removeFirst()The deque name for the same action.

pop() is the same as removeFirst() and remove(). Only the name tells the reader that you are thinking of a stack.

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

Parameters

ParameterDescription
Nonepop() takes no parameters.

Return Value

Return ValueDescription
EThe element at the top of the stack, which is the first element of the list.

Compare with Similar Methods

Stack methods and look-alikes:

MethodHow it differs
push(e)Adds the element that pop will return first.
peek()Reads the top element without removing it; null if empty.
removeFirst()The same operation under the deque name.
ArrayDeque.pop()The same method on an array-based deque, the usual stack choice.

Java Examples

Example 1 — Pop Items Off a Stack

Push three books, then pop them one by one and see the reversed order.

Java
import java.util.LinkedList;

public class LinkedListPopDemo {
    public static void main(String[] args) {
        LinkedList<String> books = new LinkedList<>();
        books.push("Java Basics");
        books.push("Data Structures");
        books.push("Algorithms");

        System.out.println("pop: " + books.pop());
        System.out.println("pop: " + books.pop());
        System.out.println("left: " + books);
    }
}

How It Works

  1. Books are stacked. Each push puts a book on top, so "Algorithms" ends up on top.
  2. pop takes the top. The first pop returns "Algorithms", the book pushed last.
  3. Then the next one. The second pop returns "Data Structures".
  4. One book left. "Java Basics", the first one pushed, is still at the bottom.

Example 2 — Reverse a Word with a Stack

Push every letter of a word, then pop them all to build the reversed word.

Java
import java.util.LinkedList;

public class LinkedListPopReverse {
    public static void main(String[] args) {
        String word = "stack";
        LinkedList<Character> letters = new LinkedList<>();
        for (char c : word.toCharArray()) {
            letters.push(c);
        }

        StringBuilder reversed = new StringBuilder();
        while (!letters.isEmpty()) {
            reversed.append(letters.pop());
        }
        System.out.println(word + " -> " + reversed);
    }
}

How It Works

  1. Push each letter. The letters s, t, a, c and k go on the stack one after another, so k ends up on top.
  2. pop gives the newest first. The first pop returns k, then c, then a, then t, then s.
  3. Appended in that order. The StringBuilder collects k, c, a, t, s.
  4. The word is reversed. The loop stops when the stack is empty, and "stack" has become "kcats".

Example 3 — Avoid Popping an Empty Stack

Pop an empty stack to see the exception, and then use a guard to avoid it.

Java
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class LinkedListPopEmpty {
    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();

        try {
            stack.pop();
        } catch (NoSuchElementException e) {
            System.out.println("pop on empty -> NoSuchElementException");
        }

        stack.push(1);
        stack.push(2);
        for (int i = 0; i < 3; i++) {
            if (stack.isEmpty()) {
                System.out.println("pop " + i + ": nothing to pop");
            } else {
                System.out.println("pop " + i + ": " + stack.pop());
            }
        }
    }
}

How It Works

  1. An empty stack. No element is on top, so pop() has nothing to return.
  2. The exception. Java throws NoSuchElementException, and the catch block prints a message.
  3. Two numbers pushed. 2 is on top of 1.
  4. The guard. The first two passes pop 2 and then 1, and the third pass sees isEmpty() and reports that there is nothing to pop.

Frequently Asked Questions

It removes and returns the first element of the list. With push, that first element is the top of the stack.
It throws a NoSuchElementException. Check isEmpty() first, or use poll() to get null.
Both remove the first element. On an empty list pop throws NoSuchElementException, and poll returns null.
push inserts at the front and pop removes from the front, so the newest element always comes out first. This is called last in, first out (LIFO).
Both work. ArrayDeque is usually faster and uses less memory, but LinkedList is fine when you also need null elements or list methods.

Next: push()

Push an element onto the front of the list, like a stack.

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

7 people found this page helpful