Home Java LinkedList Methods push() Java LinkedList push() Method 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.
LinkedList<String> stack = new LinkedList<>();
stack.push("A"); // [A]
stack.push("B"); // [B, A]
String top = stack.pop(); // "B", the last one pushed Call What 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.util.LinkedList
public void push(E e) Parameters Parameter Description e (E)The element to push. A LinkedList accepts null.
Return Value Return Value Description voidNothing is returned.
Important
Do not mix push with add when you want a stack. push inserts at the front, but add appends at the back, so the list would no longer behave in last-in, first-out order. Use push, pop and peek together.
Compare with Similar Methods Related methods:
Method How 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 1. A simple stack 2. Undo stack 3. push versus addFirst Example 1 — Push Plates onto a Stack Push three plates, look at the top one, and print the whole stack.
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());
}
} stack: [red plate, green plate, blue plate]
top: red plate
size: 3 How It Works Plates go on top. Each push inserts at the front, above the plates already there.Newest first when printed. The red plate was pushed last, so it is the first item in the output.peek reads the top. It returns "red plate" and leaves the stack unchanged.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.
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());
}
}
} undo: pasted image
undo: made text bold
undo: typed 'Hello' How It Works Actions are pushed in order. Typing happens first, bolding second, and pasting last.pop removes the top. The most recent action is always the next one to be undone.Reverse order. The output starts with the pasted image and ends with the typing.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.
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);
}
} push: [3, 2, 1]
addFirst: [3, 2, 1]
add: [1, 2, 3] How It Works Same numbers, three methods. The loop gives 1, 2 and 3 to each list in turn.push and addFirst match. Both put each new number at the front, so both lists read 3, 2, 1.add goes the other way. It appends at the back, so its list reads 1, 2, 3.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 What does LinkedList push() do? 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.
What is the difference between push() and addFirst()? None. push() just calls addFirst(). Use push when you think of a stack and addFirst when you think of a deque.
Can I use a LinkedList as a stack in Java? Yes. Use push, pop and peek. For new code, an ArrayDeque is usually the faster choice, but LinkedList works and allows null.
Why does my stack print the newest element first? Because push inserts at the front and the front is printed first. The top of the stack is the first element of the list.
What happens if I call pop() on an empty stack? It throws a NoSuchElementException. Check isEmpty() first, or use poll(), which returns null instead.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion