Reorder Linked List in DSA Rust - Time & Space Complexity
We want to understand how the time needed to reorder a linked list changes as the list gets bigger.
Specifically, we ask: how does the work grow when we rearrange nodes in this way?
Analyze the time complexity of the following code snippet.
// Reorder linked list: L0->L1->ā¦->Ln-1->Ln
// to L0->Ln->L1->Ln-1->L2->Ln-2->ā¦
fn reorder_list(head: &mut Option>) {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return;
}
// 1. Find middle
let mut slow = head.as_ref();
let mut fast = head.as_ref();
while fast.is_some() && fast.unwrap().next.is_some() {
slow = slow.unwrap().next.as_ref();
fast = fast.unwrap().next.as_ref().unwrap().next.as_ref();
}
// 2. Reverse second half
// 3. Merge two halves
}
This code finds the middle of the list, reverses the second half, then merges the two halves to reorder the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the linked list multiple times.
- How many times: Three main passes: one to find the middle, one to reverse the second half, and one to merge.
Each pass goes through about half or all of the list, so the total work grows roughly in a straight line with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~30 (3 passes x 10 nodes) |
| 100 | ~300 (3 passes x 100 nodes) |
| 1000 | ~3000 (3 passes x 1000 nodes) |
Pattern observation: The operations increase linearly as the list grows.
Time Complexity: O(n)
This means the time to reorder grows directly in proportion to the number of nodes in the list.
[X] Wrong: "Reordering the list takes more than linear time because of multiple steps."
[OK] Correct: Each step only passes through the list once or half once, so combined they still add up to a linear amount of work.
Understanding this helps you explain how to efficiently manipulate linked lists, a common skill in coding interviews and real projects.
"What if we used recursion to reorder the list instead of iteration? How would the time complexity change?"
Practice
Reorder Linked List problem?Solution
Step 1: Understand the reorder pattern
The reorder linked list problem requires rearranging nodes so the first node is followed by the last node, then the second node, then the second last node, and so on.Step 2: Differentiate from other operations
This is different from sorting, reversing, or removing duplicates because it mixes nodes from both ends alternatively.Final Answer:
Rearrange the list so nodes alternate from start and end -> Option CQuick Check:
Reorder = alternate start and end nodes [OK]
- Confusing reorder with sorting
- Thinking reorder means full reversal
- Assuming duplicates are removed
Reorder Linked List?Solution
Step 1: Understand Rust ownership for linked lists
Rust requires usingOptionfor the next pointer to allow nullable and heap allocation.Step 2: Check each option
struct Node { val: i32, next: Option } usesOptionwhich is correct. struct Node { val: i32, next: &Node } uses a reference which is invalid for ownership. struct Node { val: i32, next: Box<Node> } missesOptionso no null. struct Node { val: i32, next: Option<Node> } usesOption<Node>which is invalid because Node is not sized.Final Answer:
struct Node { val: i32, next: Option } -> Option AQuick Check:
Rust linked list node = Option [OK]
- Using references instead of Box for next
- Omitting Option causing compile errors
- Using non-boxed Node causing size errors
Solution
Step 1: Identify the reorder pattern on the list
Start with first node (1), then last node (4), then second node (2), then second last node (3).Step 2: Write the reordered list
The reordered list is 1 -> 4 -> 2 -> 3 -> null.Final Answer:
1 -> 4 -> 2 -> 3 -> null -> Option AQuick Check:
Reorder 1-2-3-4 = 1-4-2-3 [OK]
- Reversing entire list instead of alternating
- Keeping original order unchanged
- Swapping adjacent nodes incorrectly
fn reorder_list(head: &mut Option) {
let mut slow = head.as_ref();
let mut fast = head.as_ref();
while fast.is_some() && fast.unwrap().next.is_some() {
slow = slow.unwrap().next.as_ref();
fast = fast.unwrap().next.as_ref().unwrap().next.as_ref();
}
}
What is the main issue in this code?Solution
Step 1: Analyze use of as_ref()
Usingas_ref()creates an immutable reference, soslowandfastcannot be used to mutate the list later.Step 2: Understand mutation needs in reorder
Reordering requires mutable access to split and merge nodes, so immutable borrows block this.Final Answer:
Using as_ref() causes immutable borrow, preventing mutation later -> Option BQuick Check:
Immutable borrow blocks mutation in reorder [OK]
- Ignoring borrow rules causing compile errors
- Assuming unwrap always safe without checks
- Misunderstanding pointer movement speed
Solution
Step 1: Split the list into two halves
For 1->2->3->4->5, the middle is at 3, so first half: 1->2->3, second half: 4->5.Step 2: Reverse the second half
Reverse 4->5 to get 5->4.Step 3: Merge alternately
Merge nodes from first and reversed second half: 1->5->2->4->3->null.Final Answer:
1 -> 5 -> 2 -> 4 -> 3 -> null -> Option DQuick Check:
Split, reverse second half, merge = 1-5-2-4-3 [OK]
- Not reversing second half before merge
- Merging without alternating nodes
- Incorrect middle node calculation
Ready to prove it?
Pass the test and this pattern moves unknown
