Java LinkedList push() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, push(e) places the element at the front of the LinkedList. In stack terms the front is the "top", so the newest element is always the first one you can see or remove.

Behind the scenes push simply calls addFirst(e). The name makes the code easier to read when you use the list as a stack together with pop() and peek().

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

Pass the element to place on top of the stack.

Java
LinkedList<String> stack = new LinkedList<>();
stack.push("A");            // [A]
stack.push("B");            // [B, A]
String top = stack.pop();   // "B", the last one pushed
CallWhat it does
stack.push(e)Puts e on the top (front). Returns nothing.
stack.pop()Removes and returns the top element.
stack.peek()Returns the top element without removing it.
stack.addFirst(e)The deque name for the same action.

Printing a stack made with push shows the newest element first, because the top is the front of the list.

Library declarations
Java
// java.util.LinkedList
public void push(E e)

Parameters

ParameterDescription
e (E)The element to push. A LinkedList accepts null.

Return Value

Return ValueDescription
voidNothing is returned.

Compare with Similar Methods

Related methods:

MethodHow it differs
pop()Removes the element that push added last.
addFirst(e)The very same action under the deque name.
peek()Reads the top element without removing it.
ArrayDeque.push()The same stack method on an array-based deque, usually the preferred stack class.

Java Examples

Example 1 — Push Plates onto a Stack

Push three plates, look at the top one, and print the whole stack.

Java
import java.util.LinkedList;

public class LinkedListPushDemo {
    public static void main(String[] args) {
        LinkedList<String> plates = new LinkedList<>();

        plates.push("blue plate");
        plates.push("green plate");
        plates.push("red plate");

        System.out.println("stack: " + plates);
        System.out.println("top: " + plates.peek());
        System.out.println("size: " + plates.size());
    }
}

How It Works

  1. Plates go on top. Each push inserts at the front, above the plates already there.
  2. Newest first when printed. The red plate was pushed last, so it is the first item in the output.
  3. peek reads the top. It returns "red plate" and leaves the stack unchanged.
  4. Three plates in total. size() counts all three, and none was removed.

Example 2 — Build an Undo History with push and pop

Push editing actions as they happen, then undo them in reverse order.

Java
import java.util.LinkedList;

public class LinkedListPushUndo {
    public static void main(String[] args) {
        LinkedList<String> undo = new LinkedList<>();
        undo.push("typed 'Hello'");
        undo.push("made text bold");
        undo.push("pasted image");

        while (!undo.isEmpty()) {
            System.out.println("undo: " + undo.pop());
        }
    }
}

How It Works

  1. Actions are pushed in order. Typing happens first, bolding second, and pasting last.
  2. pop removes the top. The most recent action is always the next one to be undone.
  3. Reverse order. The output starts with the pasted image and ends with the typing.
  4. An empty list ends the loop. After three pops the list has no elements left, so isEmpty() becomes true.

Example 3 — Compare push, addFirst and add

Use three methods and see which ones put an element at the front.

Java
import java.util.LinkedList;

public class LinkedListPushCompare {
    public static void main(String[] args) {
        LinkedList<Integer> viaPush = new LinkedList<>();
        LinkedList<Integer> viaAddFirst = new LinkedList<>();
        LinkedList<Integer> viaAdd = new LinkedList<>();

        for (int i = 1; i <= 3; i++) {
            viaPush.push(i);
            viaAddFirst.addFirst(i);
            viaAdd.add(i);
        }

        System.out.println("push:     " + viaPush);
        System.out.println("addFirst: " + viaAddFirst);
        System.out.println("add:      " + viaAdd);
    }
}

How It Works

  1. Same numbers, three methods. The loop gives 1, 2 and 3 to each list in turn.
  2. push and addFirst match. Both put each new number at the front, so both lists read 3, 2, 1.
  3. add goes the other way. It appends at the back, so its list reads 1, 2, 3.
  4. A stack needs the front. That is why a stack made with push should be read with pop and peek, which work on the front.

Frequently Asked Questions

It inserts the element at the front of the list. When the list is used as a stack, the front is the top of the stack.
None. push() just calls addFirst(). Use push when you think of a stack and addFirst when you think of a deque.
Yes. Use push, pop and peek. For new code, an ArrayDeque is usually the faster choice, but LinkedList works and allows null.
Because push inserts at the front and the front is printed first. The top of the stack is the first element of the list.
It throws a NoSuchElementException. Check isEmpty() first, or use poll(), which returns null instead.

Next: remove()

Remove the head, the element at an index, or the first match.

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

8 people found this page helpful