Challenge - 5 Problems
Reorder Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Reordering a Linked List
What is the printed state of the linked list after reordering it with the given Rust code?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn reorder_list(head: &mut Option<Box<ListNode>>) {
let mut vals = Vec::new();
let mut current = head.as_ref();
while let Some(node) = current {
vals.push(node.val);
current = node.next.as_ref();
}
let mut i = 0;
let mut j = vals.len() - 1;
let mut current_mut = head.as_mut();
while i <= j {
if let Some(node) = current_mut {
node.val = vals[i];
i += 1;
if i > j { break; }
if let Some(next_node) = node.next.as_mut() {
next_node.val = vals[j];
j -= 1;
current_mut = next_node.next.as_mut();
} else {
break;
}
} else {
break;
}
}
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head.as_ref();
let mut result = Vec::new();
while let Some(node) = current {
result.push(node.val);
current = node.next.as_ref();
}
println!("{:?}", result);
}
fn main() {
let mut head = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None })) })) })) }));
reorder_list(&mut head);
print_list(&head);
}Attempts:
2 left
💡 Hint
Think about how the reorder_list function rearranges the values from the ends towards the center.
✗ Incorrect
The reorder_list function collects all values, then alternates assigning from the start and end of the list, resulting in the order 1,4,2,3.
🧠 Conceptual
intermediateUnderstanding Reorder Linked List Algorithm
Which step is NOT part of the common approach to reorder a linked list as L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ...?
Attempts:
2 left
💡 Hint
Reordering does not require sorting the list.
✗ Incorrect
The reorder algorithm involves finding the middle, reversing the second half, and merging alternately. Sorting is not required.
🔧 Debug
advancedIdentify the Bug in Reorder Linked List Code
What error will this Rust code cause when trying to reorder a linked list?
DSA Rust
fn reorder_list(head: &mut Option<Box<ListNode>>) {
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();
}
// Further code omitted
}Attempts:
2 left
💡 Hint
Check unwrap calls carefully for possible None values.
✗ Incorrect
The unwrap calls on fast and slow pointers can panic if next is None, causing runtime panic.
📝 Syntax
advancedCorrect Syntax to Reverse a Linked List in Rust
Which option correctly reverses a singly linked list in Rust?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
// Fill in the blank
}Attempts:
2 left
💡 Hint
You need to take ownership of node.next to modify it safely.
✗ Incorrect
Option B correctly uses node.next.take() to move ownership and reassign pointers safely.
🚀 Application
expertFinal Order of Nodes After Reordering
Given the linked list 10 → 20 → 30 → 40 → 50 → 60, what is the order of nodes after applying the reorder list algorithm?
Attempts:
2 left
💡 Hint
The reorder alternates nodes from the start and end moving inward.
✗ Incorrect
The reordered list alternates first and last nodes: 10,60,20,50,30,40.
