Stack Implementation Using Linked List in DSA Go - Time & Space Complexity
We want to understand how fast stack operations work 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 and pop operations.
// Node represents an element in the stack
type Node struct {
value int
next *Node
}
// Stack holds the top node
type Stack struct {
top *Node
}
// Push adds a value on top
func (s *Stack) Push(val int) {
newNode := &Node{value: val, next: s.top}
s.top = newNode
}
// Pop removes and returns the top value
func (s *Stack) Pop() (int, bool) {
if s.top == nil {
return 0, false
}
val := s.top.value
s.top = s.top.next
return val, true
}
This code adds or removes elements at the top of the stack using a linked list.
Look for loops or repeated steps in push and pop.
- Primary operation: Assigning pointers to add or remove the top node.
- How many times: Each push or pop does this once, no loops involved.
Each push or pop takes the same steps no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 pointer change |
| 100 | 1 pointer change |
| 1000 | 1 pointer change |
Pattern observation: The time stays the same even if the stack grows.
Time Complexity: O(1)
This means push and pop take the same short time no matter how many items are in the stack.
[X] Wrong: "Push or pop takes longer as the stack grows because it has more elements."
[OK] Correct: Each operation only changes the top pointer once, so size does not affect time.
Knowing that stack operations are quick and constant time helps you explain efficient data handling in interviews.
"What if we changed the stack to use an array instead of a linked list? How would the time complexity of push and pop change?"
Practice
Solution
Step 1: Understand stack size limitation in arrays
Arrays have fixed size, so stack implemented with arrays has a fixed maximum capacity.Step 2: Linked list allows dynamic memory allocation
Linked list nodes are created as needed, so stack size can grow or shrink dynamically without fixed limit.Final Answer:
Dynamic size, no fixed limit on number of elements -> Option CQuick Check:
Linked list stack size is dynamic = B [OK]
- Thinking linked list allows random access
- Confusing memory usage for small stacks
- Assuming linked list is always faster
Solution
Step 1: Understand pointer usage in linked list nodes
Each node must point to the next node using a pointer type to avoid copying entire struct.Step 2: Check each option for correct pointer declaration
type Node struct { data int; next *Node } uses *Node pointer for next, which is correct. type Node struct { data int; next Node } uses Node (value), which causes copying. type Node struct { data *int; next *Node } uses *int for data which is unnecessary. type Node struct { data int; next interface{} } uses interface{} which is not type safe.Final Answer:
type Node struct { data int; next *Node } -> Option BQuick Check:
Next pointer must be *Node = A [OK]
- Using value type instead of pointer for next
- Using pointer for data unnecessarily
- Using interface{} instead of concrete type
package main
import "fmt"
type Node struct {
data int
next *Node
}
var top *Node = nil
func push(val int) {
newNode := &Node{data: val, next: top}
top = newNode
}
func pop() int {
if top == nil {
return -1
}
val := top.data
top = top.next
return val
}
func main() {
push(10)
push(20)
push(30)
fmt.Println(pop())
fmt.Println(pop())
}Solution
Step 1: Trace push operations
push(10) makes top point to node with 10; push(20) makes top point to node with 20 -> 10; push(30) makes top point to node with 30 -> 20 -> 10.Step 2: Trace pop operations
First pop returns 30 and moves top to 20 -> 10; second pop returns 20 and moves top to 10.Final Answer:
30 20 -> Option AQuick Check:
Stack LIFO order = 30 then 20 [OK]
- Confusing push order with pop order
- Returning wrong node data on pop
- Not updating top pointer after pop
func pop() int {
if top.next == nil {
return -1
}
val := top.data
top = top.next
return val
}Solution
Step 1: Analyze nil checks in pop function
The function checks if top.next == nil but does not check if top itself is nil, which can cause runtime panic if stack is empty.Step 2: Understand correct empty stack check
Correct check is if top == nil to detect empty stack before accessing top.next.Final Answer:
It does not check if top is nil before accessing top.next -> Option AQuick Check:
Check top == nil before top.next [OK]
- Checking top.next before top
- Returning wrong error value
- Forgetting to update top pointer
Solution
Step 1: Understand constraints of no extra data structures
Using only push and pop means no arrays or new stacks allowed; recursion uses call stack implicitly.Step 2: Use recursion to pop all elements and then push back in reverse
Recursively pop elements until stack is empty, then insert elements at bottom recursively to reverse order.Final Answer:
Use recursion: pop all elements, then push them back in reverse order -> Option DQuick Check:
Recursion reverses stack without extra data structures [OK]
- Using extra arrays or stacks violating constraints
- Trying to swap node data directly
- Not handling empty stack base case in recursion
Ready to prove it?
Pass the test and this pattern moves unknown
