Stack Implementation Using Linked List in DSA Rust - Time & Space Complexity
We want to understand how fast the main operations of a stack built with a linked list run.
Specifically, how the time to add or remove items changes as the stack grows.
Analyze the time complexity of the following code snippet.
struct Node {
data: i32,
next: Option>,
}
struct Stack {
top: Option>,
}
impl Stack {
fn push(&mut self, value: i32) {
let new_node = Box::new(Node { data: value, next: self.top.take() });
self.top = Some(new_node);
}
fn pop(&mut self) -> Option {
self.top.take().map(|node| {
self.top = node.next;
node.data
})
}
}
This code adds (push) and removes (pop) items at the top of a linked list to behave like a stack.
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 traversals involved.
Each push or pop works only on the top node, so the time does not increase as the stack grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per push/pop |
| 100 | 1 operation per push/pop |
| 1000 | 1 operation per push/pop |
Pattern observation: 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 must take longer as the stack grows because there are more items."
[OK] Correct: We only work with the top item, so the rest of the stack does not slow us down.
Knowing that stack operations are fast and constant time helps you explain why stacks are good for quick add/remove tasks.
"What if we changed the stack to add items at the end of the linked list instead of the front? How would the time complexity change?"
Practice
Solution
Step 1: Understand stack size limitation in arrays
Arrays have fixed size, so stacks implemented with arrays have 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 CQuick Check:
Dynamic size = Advantage of linked list stack [OK]
- Thinking linked list uses less memory always
- Confusing stack with queue behavior
- Assuming random access is possible in linked list
Solution
Step 1: Understand Rust ownership and recursive types
Recursive types like linked lists require indirection using Box and Option for nullable pointers.Step 2: Analyze each option
struct Node { data: i32, next: Option } uses Option which allows nullable next pointer; struct Node { data: i32, next: Node } and struct Node { data: i32, next: &Node } are invalid due to recursive type without indirection; struct Node { data: i32, next: Box<Node> } misses Option, so no null pointer.Final Answer:
struct Node { data: i32, next: Option } -> Option AQuick Check:
Option = correct linked list pointer [OK]
- Omitting Option causing infinite size error
- Using references (&Node) without lifetime
- Using Box<Node> without Option for nullable
struct Node {
data: i32,
next: Option,
}
struct Stack {
head: Option,
}
impl Stack {
fn push(&mut self, val: i32) {
let new_node = Box::new(Node { data: val, next: self.head.take() });
self.head = Some(new_node);
}
}
fn main() {
let mut stack = Stack { head: None };
stack.push(10);
stack.push(20);
stack.push(30);
// What is the top element's data now?
println!("{}", stack.head.as_ref().unwrap().data);
}Solution
Step 1: Trace push operations
Push 10: head points to node with data 10; Push 20: head points to node with data 20, next is 10; Push 30: head points to node with data 30, next is 20.Step 2: Identify top element
After pushes, top element is the head node's data, which is 30.Final Answer:
30 -> Option AQuick Check:
Last pushed = top element = 30 [OK]
- Confusing order of nodes in linked list
- Assuming head points to bottom element
- Not using take() causing borrow errors
impl Stack {
fn pop(&mut self) -> Option<i32> {
if self.head.is_none() {
return None;
}
let node = self.head.unwrap();
self.head = node.next;
Some(node.data)
}
}Solution
Step 1: Analyze unwrap() usage on self.head
unwrap() consumes the Option, but self.head is borrowed mutably, causing ownership error.Step 2: Correct way is to use take() to move ownership
Using self.head.take() moves ownership out safely, allowing access to node and updating head.Final Answer:
Using unwrap() on Option without handling ownership -> Option DQuick Check:
unwrap() consumes Option, use take() instead [OK]
- Using unwrap() causing borrow checker errors
- Ignoring empty stack check
- Confusing references and ownership
reverse_stack that reverses the elements of a linked list stack without using extra data structures. Which approach correctly achieves this in Rust?Solution
Step 1: Understand constraints - no extra data structures allowed
Options A and D use extra stacks or vectors, so they violate constraints.Step 2: Recursive approach explanation
Recursively pop elements until stack is empty, then push elements back in reverse order, effectively reversing stack without extra storage.Step 3: Swapping data values is complex and error-prone
Traverse linked list and swap data values between nodes from head to tail requires traversal and swapping data, which is complicated and not idiomatic in Rust linked lists.Final Answer:
Recursively pop elements, reverse the rest, then push popped element back -> Option BQuick Check:
Recursion reverses stack without extra structures [OK]
- Using extra data structures despite constraints
- Trying to swap node data directly
- Ignoring ownership rules in recursion
Ready to prove it?
Pass the test and this pattern moves unknown
