Why Doubly Linked List Over Singly Linked List in DSA Rust - Performance Analysis
We want to understand how using a doubly linked list changes the time it takes to do common tasks compared to a singly linked list.
Which operations become faster or slower and why?
Analyze the time complexity of removing a node from the middle of a doubly linked list.
struct Node {
value: i32,
prev: Option<*mut Node>,
next: Option>,
}
fn remove_node(node: &mut Node) {
unsafe {
if let Some(prev_node) = node.prev {
(*prev_node).next = node.next.take();
}
if let Some(ref mut next_node) = node.next {
next_node.prev = node.prev;
}
}
}
This code removes a node by updating the previous and next nodes to skip it.
Look for loops or repeated steps that take time.
- Primary operation: Updating pointers of previous and next nodes.
- How many times: Constant time, only once per removal.
Removing a node takes the same steps no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 pointer updates |
| 100 | 3 pointer updates |
| 1000 | 3 pointer updates |
Pattern observation: The work stays the same even if the list grows.
Time Complexity: O(1)
This means removing a node is done in constant time, no matter the list size.
[X] Wrong: "Removing a node in a doubly linked list always takes longer because it has more pointers to update."
[OK] Correct: Actually, having both previous and next pointers lets us remove a node directly without searching, so it is faster or the same time as singly linked lists.
Knowing why doubly linked lists can remove nodes faster shows you understand how data structure design affects speed, a key skill in coding interviews.
"What if we only had a singly linked list and wanted to remove a node given only that node? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal capabilities
A singly linked list allows traversal only in one direction, from head to tail.Step 2: Compare with doubly linked list
A doubly linked list stores two pointers per node, allowing traversal both forward and backward.Final Answer:
Because it allows traversal in both forward and backward directions -> Option BQuick Check:
Traversal direction = Both forward and backward [OK]
- Thinking doubly linked lists use less memory
- Confusing pointer count per node
- Assuming singly linked lists allow backward traversal
Solution
Step 1: Identify doubly linked list node fields
A doubly linked list node must have pointers to both previous and next nodes, usually wrapped in Option and Box for ownership.Step 2: Check each option
struct Node { data: i32, prev: Option, next: Option } correctly declares both prev and next as Option. Others miss prev or use incorrect types.Final Answer:
struct Node { data: i32, prev: Option, next: Option } -> Option AQuick Check:
Prev and next pointers = struct Node { data: i32, prev: Option, next: Option } [OK]
- Omitting prev pointer
- Using Box<Node> without Option
- Using next as Node instead of pointer
let mut list = DoublyLinkedList::new(); list.push_back(10); list.push_back(20); list.push_back(30); list.print_forward();
Solution
Step 1: Understand push_back behavior
push_back adds nodes at the end, so nodes are appended in order 10, then 20, then 30.Step 2: Trace print_forward output
Printing forward shows nodes from head to tail: 10 -> 20 -> 30 -> null.Final Answer:
10 -> 20 -> 30 -> null -> Option DQuick Check:
Insertion order = Printed order [OK]
- Confusing push_back with push_front
- Printing backward instead of forward
- Mixing node order in output
fn print_backward(&self) {
let mut current = self.tail.clone();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.clone();
}
println!("null");
}Solution
Step 1: Analyze traversal direction
Backward traversal should follow prev pointers, but code uses node.next which goes forward.Step 2: Identify correct pointer usage
Replacing node.next with node.prev fixes backward traversal.Final Answer:
Using node.next instead of node.prev for backward traversal -> Option CQuick Check:
Backward traversal = Use prev pointer [OK]
- Confusing next and prev pointers
- Not cloning pointers properly
- Printing wrong data fields
Solution
Step 1: Understand node removal requirements
Removing a node requires updating pointers of its neighbors, including the previous node.Step 2: Compare singly and doubly linked lists
Singly linked lists lack backward pointers, so to remove a node, you must traverse from head to find the previous node. Doubly linked lists have prev pointers, allowing direct access to neighbors.Final Answer:
Because doubly linked list nodes have pointers to previous nodes, enabling direct access to neighbors -> Option AQuick Check:
Prev pointer enables quick removal [OK]
- Assuming singly linked lists have backward pointers
- Thinking memory usage affects removal speed
- Confusing traversal direction capabilities
Ready to prove it?
Pass the test and this pattern moves unknown
