Stack Implementation Using Linked List in DSA Java - Time & Space Complexity
We want to understand how fast stack operations run when using a linked list.
How does the time to push or pop change as the stack grows?
Analyze the time complexity of the following stack push operation.
class Node {
int data;
Node next;
Node(int data) { this.data = data; this.next = null; }
}
class Stack {
Node top;
void push(int data) {
Node newNode = new Node(data);
newNode.next = top;
top = newNode;
}
}
This code adds a new item on top of the stack using a linked list.
Look for loops or repeated steps in the push operation.
- Primary operation: Creating a new node and linking it to the top.
- How many times: Exactly once per push call, no loops involved.
Each push does the same small set of steps no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (create node, link, update top) |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The number of steps stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means pushing an item takes the same short time no matter how big the stack is.
[X] Wrong: "Pushing takes longer as the stack grows because we have to move through all nodes."
[OK] Correct: We only change the top pointer and link the new node; we never walk through the list.
Knowing that stack push and pop are quick helps you explain why stacks are good for fast temporary storage.
"What if we used an array instead of a linked list for the stack? How would the time complexity of push change?"
Practice
Solution
Step 1: Understand stack size limitation in arrays
Arrays have a fixed size, so stacks implemented with arrays have a maximum capacity.Step 2: Linked list dynamic size advantage
Linked lists can grow or shrink dynamically by adding or removing nodes, so the stack size is limited only by memory.Final Answer:
The stack can grow dynamically without a fixed size limit -> Option BQuick Check:
Dynamic size = Advantage of linked list stack [OK]
- Thinking linked list uses less memory always
- Assuming stacks allow random access
- Confusing sorting with stack operations
Solution
Step 1: Identify node structure for linked list stack
A node should store data and a reference to the next node.Step 2: Check each option
class Node { int data; Node next; } correctly uses 'int data' and 'Node next'. class Node { int data; Node prev; } uses 'prev' which is for doubly linked lists. class Node { int data; int next; } wrongly uses 'int next'. class Node { Node data; Node next; } wrongly uses 'Node data'.Final Answer:
class Node { int data; Node next; } -> Option DQuick Check:
Node stores data + next pointer [OK]
- Using 'prev' pointer in singly linked list
- Declaring next as int instead of Node
- Using Node type for data field
push(10); push(20); push(30); pop(); push(40);
Print the stack from top to bottom.
Solution
Step 1: Trace push and pop operations
push(10) -> stack: 10
push(20) -> stack: 20 -> 10
push(30) -> stack: 30 -> 20 -> 10
pop() removes 30 -> stack: 20 -> 10
push(40) -> stack: 40 -> 20 -> 10Step 2: Write final stack order
Top is 40, then 20, then 10, then null.Final Answer:
40 -> 20 -> 10 -> null -> Option CQuick Check:
Stack top after ops = 40 -> 20 -> 10 [OK]
- Forgetting pop removes top element
- Appending instead of prepending on push
- Confusing order of elements after operations
public int pop() {
if (top == null) {
System.out.println("Stack Underflow");
}
int value = top.data;
top = top.next;
return value;
}Solution
Step 1: Check empty stack condition handling
The method prints "Stack Underflow" but continues to access top.data without stopping or returning.Step 2: Consequence of missing return or exception
Accessing top.data when top is null causes NullPointerException. Proper handling requires returning or throwing after printing.Final Answer:
It does not handle empty stack properly before accessing top.data -> Option AQuick Check:
Null check must prevent access [OK]
- Not returning or throwing after underflow message
- Confusing overflow with underflow
- Updating wrong pointer in pop
reverseStack() that reverses the elements of a linked list stack without using extra data structures. Which approach is best?Solution
Step 1: Understand constraints of no extra data structures
Options C and D use extra structures (new list or queue), so they are invalid.Step 2: Evaluate recursion and swapping
Swapping data pairwise is complex and error-prone for linked list stack. Recursion can pop all elements and push back in reverse order naturally.Final Answer:
Use recursion to pop all elements and push them back in reverse order -> Option AQuick Check:
Recursion reverses stack without extra storage [OK]
- Using extra data structures despite constraints
- Trying to swap node data instead of nodes
- Confusing queue operations with stack reversal
Ready to prove it?
Pass the test and this pattern moves unknown
