Reverse a Doubly Linked List in DSA Rust - Time & Space Complexity
We want to understand how the time needed to reverse a doubly linked list changes as the list grows.
How does the work increase when the list gets longer?
Analyze the time complexity of the following code snippet.
struct Node {
data: i32,
prev: Option<*mut Node>,
next: Option>,
}
fn reverse(head: &mut Option>) {
let mut current = head.take();
let mut prev = None;
while let Some(mut node) = current {
let next = node.next.take();
node.next = prev.take();
node.prev = next.as_deref_mut().map(|n| &mut **n as *mut _);
prev = Some(node);
current = next;
}
*head = prev;
}
This code reverses the links of a doubly linked list by swapping the next and prev pointers for each node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so as many times as there are nodes in the list.
As the list size grows, the number of steps grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to reverse the list grows directly with the list size; doubling the list doubles the work.
[X] Wrong: "Reversing a doubly linked list takes more than linear time because of the two pointers per node."
[OK] Correct: Each node is visited once, and swapping pointers is a constant-time action, so the total time is still linear.
Understanding this helps you show you can work with linked lists efficiently, a common skill interviewers look for.
"What if we changed the list to a singly linked list? How would the time complexity of reversing it change?"
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected both forward and backward.Step 2: Purpose of reversing
Reversing swaps the direction so the last node becomes the first and the first becomes the last.Final Answer:
To change the order of nodes so the last becomes first and vice versa -> Option AQuick Check:
Reversing means flipping order = C [OK]
- Confusing reversing with deleting nodes
- Thinking reversing sorts the list
- Mixing up adding nodes with reversing
next and prev pointers of a node in a doubly linked list?Solution
Step 1: Identify correct swapping logic
Swapping means storing one pointer temporarily, then exchanging values.Step 2: Check each option
let temp = node.next; node.next = node.prev; node.prev = temp; correctly uses a temp variable to swapnextandprev. Others either assign incorrectly or use wrong variables.Final Answer:
let temp = node.next; node.next = node.prev; node.prev = temp; -> Option DQuick Check:
Swap pointers with temp variable = D [OK]
- Assigning pointers without temp causes data loss
- Mixing up
nextandprevin assignments - Using wrong pointer in swap
struct Node {
data: i32,
prev: Option,
next: Option,
}
// Assume list is 1 <-> 2 <-> 3
// After reversal, print from head
// Output format: data values separated by spaces
// Output?Solution
Step 1: Understand initial list order
The list is 1 <-> 2 <-> 3, so printing from head gives 1 2 3.Step 2: Effect of reversal
Reversing swapsnextandprevpointers, so the new head is 3, and order is 3 2 1.Final Answer:
3 2 1 -> Option AQuick Check:
Reversed list prints backwards = 3 2 1 [OK]
- Printing original order after reversal
- Confusing node data order
- Assuming reversal sorts nodes
fn reverse(mut head: Option) -> Option {
let mut current = head;
while let Some(mut node) = current {
let temp = node.next;
node.next = node.prev;
node.prev = temp;
current = node.next;
}
head
}Solution
Step 1: Analyze pointer swapping
Swappingnextandprevis done correctly inside the loop.Step 2: Check head update
The function returns original head without updating it to the new first node after reversal, so head remains old start.Final Answer:
The function does not update head to the new first node after reversal -> Option CQuick Check:
Head must point to new first node after reverse = A [OK]
- Not updating head pointer after reversal
- Returning old head instead of new head
- Incorrect loop condition causing infinite loop
Solution
Step 1: Understand position change after reversal
Reversing changes node positions, so original even positions become different in reversed list.Step 2: Correct approach
Sum nodes at even positions before reversal to get original even-position sum, then reverse list if needed.Final Answer:
Traverse original list, sum nodes at even positions, then reverse the list -> Option BQuick Check:
Sum original even positions before reversing = A [OK]
- Summing positions after reversal which changes order
- Ignoring position changes due to reversal
- Summing all nodes without filtering positions
Ready to prove it?
Pass the test and this pattern moves unknown
