Home Java LinkedList Methods pop() Java LinkedList pop() Method 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.
LinkedList<String> stack = new LinkedList<>();
stack.push("A");
stack.push("B");
String top = stack.pop(); // "B"
String next = stack.pop(); // "A" Call What 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.util.LinkedList
public E pop() Parameters Parameter Description None pop() takes no parameters.
Return Value Return Value Description EThe element at the top of the stack, which is the first element of the list.
Important
Popping an empty stack throws NoSuchElementException. Check isEmpty() first, or use poll() to get null. Always add to the stack with push. If you use add, new items go to the back, and pop will return the oldest item, not the newest.
Compare with Similar Methods Stack methods and look-alikes:
Method How 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 1. Last in, first out 2. Reverse a word 3. Empty stack Example 1 — Pop Items Off a Stack Push three books, then pop them one by one and see the reversed order.
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);
}
} pop: Algorithms
pop: Data Structures
left: [Java Basics] How It Works Books are stacked. Each push puts a book on top, so "Algorithms" ends up on top.pop takes the top. The first pop returns "Algorithms", the book pushed last.Then the next one. The second pop returns "Data Structures".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.
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 Push each letter. The letters s, t, a, c and k go on the stack one after another, so k ends up on top.pop gives the newest first. The first pop returns k, then c, then a, then t, then s.Appended in that order. The StringBuilder collects k, c, a, t, s.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.
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());
}
}
}
} pop on empty -> NoSuchElementException
pop 0: 2
pop 1: 1
pop 2: nothing to pop How It Works An empty stack. No element is on top, so pop() has nothing to return.The exception. Java throws NoSuchElementException, and the catch block prints a message.Two numbers pushed. 2 is on top of 1.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 What does LinkedList pop() do? It removes and returns the first element of the list. With push, that first element is the top of the stack.
What happens if I call pop() on an empty LinkedList? It throws a NoSuchElementException. Check isEmpty() first, or use poll() to get null.
What is the difference between pop() and poll()? Both remove the first element. On an empty list pop throws NoSuchElementException, and poll returns null.
Why does pop() return the last pushed element? 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).
Should I use LinkedList or ArrayDeque as a stack? 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 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.
7 people found this page helpful
Helpful Share Copy link Suggestion