Stack Implementation Using Linked List in DSA Typescript - 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 code snippet.
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class Stack {
top: Node | null = null;
push(value: number) {
const newNode = new Node(value);
newNode.next = this.top;
this.top = newNode;
}
pop(): number | null {
if (!this.top) return null;
const poppedValue = this.top.value;
this.top = this.top.next;
return poppedValue;
}
}
This code adds and removes items from the top of a stack using a linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one node at the top of the linked list.
- How many times: Each push or pop does this once, no loops or recursion involved.
Each push or pop takes the same small number of steps no matter how many items are in the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 steps per push or pop |
| 100 | About 3 steps per push or pop |
| 1000 | About 3 steps per push or pop |
Pattern observation: The time stays the same no matter how big the stack gets.
Time Complexity: O(1)
This means each push or pop operation takes a constant amount of time regardless of stack size.
[X] Wrong: "Push or pop takes longer as the stack grows because the list gets bigger."
[OK] Correct: Push and pop only change the top node, so they do not need to look at the whole list.
Knowing that stack operations are fast 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 a fixed size, so stacks implemented with arrays have a maximum capacity.Step 2: Linked list dynamic size
Linked lists can grow or shrink dynamically by adding or removing nodes, so stack size is not fixed.Final Answer:
The stack can grow dynamically without a fixed size limit -> Option CQuick Check:
Linked list stacks grow dynamically = A [OK]
- Thinking linked lists use less memory always
- Confusing stack with queue behavior
- Assuming random access is possible in linked lists
Solution
Step 1: Check next pointer type
In a linked list, next can be another node or null if it's the last node.Step 2: Verify constructor initialization
The constructor sets value and initializes next to null.Final Answer:
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option BQuick Check:
next can be Node or null = B [OK]
- Not allowing next to be null
- Initializing next as undefined
- Using incorrect type for next
Solution
Step 1: Understand push operation
Push adds a new node at the top, so the last pushed value becomes the top.Step 2: Trace pushing 10 then 20
First push 10: top.value = 10; then push 20: top.value = 20 (new node).Final Answer:
20 -> Option DQuick Check:
Last pushed value is top = 20 [OK]
- Assuming bottom value is top
- Confusing push with pop
- Not updating top pointer
pop(): number | null {
if (!this.top) return null;
const value = this.top.value;
this.top = this.top.next;
return this.top.value;
}Solution
Step 1: Analyze return value
The method returns this.top.value after updating top, which is the new top, not the popped value.Step 2: Correct return value
It should return the saved value before updating top to return the popped element.Final Answer:
Returns the new top value instead of popped value -> Option AQuick Check:
Pop must return popped value, not new top [OK]
- Returning top after update instead of popped value
- Not handling empty stack
- Forgetting to update top pointer
peek() in your linked list stack that returns the top value without removing it. Which is the correct implementation?Solution
Step 1: Understand peek operation
Peek returns the top value without changing the stack.Step 2: Check each option
peek(): number | null { return this.top ? this.top.value : null; } returns top value if exists, else null, without modifying stack. Others modify top or return wrong node.Final Answer:
peek(): number | null { return this.top ? this.top.value : null; } -> Option AQuick Check:
Peek returns top value without removal = C [OK]
- Modifying top pointer in peek
- Returning next node's value
- Not handling empty stack
Ready to prove it?
Pass the test and this pattern moves unknown
