Stack Implementation Using Linked List in DSA Javascript - Time & Space Complexity
We want to understand how fast stack operations run when using a linked list.
How does the time to add or remove items change as the stack grows?
Analyze the time complexity of the following stack operations using a linked list.
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
}
push(value) {
const newNode = new Node(value);
newNode.next = this.top;
this.top = newNode;
}
pop() {
if (!this.top) return null;
const poppedValue = this.top.value;
this.top = this.top.next;
return poppedValue;
}
}
This code adds (push) and removes (pop) items 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 a fixed number of steps, no loops.
Each push or pop takes the same small number of steps no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3-4 steps per push or pop |
| 100 | 3-4 steps per push or pop |
| 1000 | 3-4 steps per push or pop |
Pattern observation: The time stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means adding or removing an item takes the same short time regardless of stack size.
[X] Wrong: "Pop or push takes longer as the stack grows because it has more items."
[OK] Correct: Each operation only changes the top pointer, so it does not look at all items.
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 change?"
Practice
Solution
Step 1: Understand stack size limitation in arrays
Arrays have fixed size or require resizing, which can be costly.Step 2: Linked list dynamic size advantage
Linked lists grow and shrink dynamically without fixed size limits.Final Answer:
Dynamic size, no fixed limit on number of elements -> Option AQuick Check:
Linked list stacks grow dynamically [OK]
- Thinking linked lists allow random access
- Confusing memory usage benefits
- Assuming arrays are always faster
Solution
Step 1: Identify correct object property names
Standard node uses 'value' and 'next' properties.Step 2: Check JavaScript object literal syntax
const node = { value: val, next: null }; correctly creates an object with 'value' and 'next' keys.Final Answer:
const node = { value: val, next: null }; -> Option CQuick Check:
Object with value and next keys [OK]
- Using incorrect property names like 'val' or 'nextNode'
- Calling Node as a function without 'new'
- Missing 'const' or wrong syntax
Solution
Step 1: Push 10, 20, 30 in order
Stack after pushes: top -> 30 -> 20 -> 10 -> nullStep 2: Pop once removes top (30)
New top after pop is 20.Final Answer:
Stack top value is 20 -> Option DQuick Check:
Pop removes 30, next top is 20 [OK]
- Thinking pop removes bottom element
- Confusing order of pushes
- Assuming stack becomes empty after one pop
pop() {
if (!this.top) return null;
const poppedNode = this.top;
this.top = poppedNode.next;
return poppedNode.value;
}Solution
Step 1: Analyze pop method logic
Pop removes top node and returns its value correctly.Step 2: Check for missing stack size update
Stack size counter is not decremented, causing size mismatch.Final Answer:
Should decrement stack size counter after pop -> Option BQuick Check:
Pop must update size counter [OK]
- Thinking next pointer must be cleared
- Assuming code returns wrong value
- Ignoring size counter updates
peek() method that returns the top element without removing it? Choose the correct implementation.Solution
Step 1: Understand peek method purpose
Peek returns top value without changing stack.Step 2: Evaluate each option
peek() { return this.top ? this.top.value : null; } returns top value safely without modifying stack; others modify or access wrong nodes.Final Answer:
peek() { return this.top ? this.top.value : null; } -> Option AQuick Check:
Peek returns top value without removal [OK]
- Using pop inside peek causing removal
- Accessing next node instead of top
- Changing top pointer in peek
Ready to prove it?
Pass the test and this pattern moves unknown
