Stack Implementation Using Linked List in DSA C++ - 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 push operation in this stack implementation.
struct Node {
int data;
Node* next;
};
class Stack {
Node* top = nullptr;
public:
void push(int val) {
Node* newNode = new Node{val, top};
top = newNode;
}
};
This code adds a new element to the top of the stack using a linked list.
Look for loops or repeated steps inside push.
- Primary operation: Creating a new node and linking it to the current top.
- How many times: Exactly once per push call, no loops involved.
Each push does the same small work no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 push = 1 operation |
| 100 | 1 push = 1 operation |
| 1000 | 1 push = 1 operation |
Pattern observation: The work stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means each push operation takes the same small amount of time regardless of stack size.
[X] Wrong: "Push takes longer as the stack grows because it has to move all elements."
[OK] Correct: The linked list lets us add a new node at the front instantly without moving others.
Knowing that stack push and pop are fast and constant time helps you explain efficient data handling in interviews.
"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 fixed size, so stack implemented with arrays has a maximum capacity.Step 2: Linked list dynamic size property
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 BQuick Check:
Linked list stack size = dynamic growth [OK]
- Thinking linked list uses less memory always
- Assuming stack allows random access
- Confusing sorting with stack operations
Solution
Step 1: Understand dynamic memory allocation in C++
To create a node dynamically, use 'new' keyword and pointer type.Step 2: Check syntax correctness
Node* newNode = new Node(value); uses pointer and 'new' correctly: Node* newNode = new Node(value);Final Answer:
Node* newNode = new Node(value); -> Option AQuick Check:
Dynamic node creation = new + pointer [OK]
- Omitting pointer (*) when using new
- Using stack allocation instead of dynamic
- Assigning new object to non-pointer variable
Stack s; s.push(10); s.push(20); s.push(30); s.pop(); s.push(40); s.display();
Assuming display prints elements 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->10.Step 2: Determine display output
Display prints from top to bottom: 40 -> 20 -> 10 -> null.Final Answer:
40 -> 20 -> 10 -> null -> Option DQuick Check:
Stack after ops = 40->20->10 [OK]
- Forgetting pop removes top element
- Assuming push adds at bottom
- Mixing order of elements in display
int pop() {
if (top == NULL) {
cout << "Stack Underflow";
return -1;
}
int val = top->data;
delete top;
top = top->next;
return val;
}Solution
Step 1: Analyze order of operations in pop
Code deletes top node before accessing top->next, which is unsafe.Step 2: Identify correct order
Must save top->next in a temp pointer before deleting top, then update top.Final Answer:
Accessing top->next after deleting top causes undefined behavior -> Option CQuick Check:
Delete before next access = error [OK]
- Deleting node before updating pointer
- Ignoring stack underflow check
- Returning wrong values on empty stack
Solution
Step 1: Understand constraints
No extra data structures allowed, so cannot use queue or new list.Step 2: Use recursion to reverse stack
Recursion can pop all elements, then push them back in reverse order naturally.Step 3: Why other options fail
Swapping data pairwise does not reverse entire stack; copying needs extra list; queue is extra structure.Final Answer:
Use recursion to pop all elements and push them back in reverse order -> Option AQuick Check:
Recursion reverses stack without extra structures [OK]
- Using extra data structures despite constraints
- Trying to swap data instead of nodes
- Not understanding recursion stack behavior
Ready to prove it?
Pass the test and this pattern moves unknown
