Challenge - 5 Problems
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing a linked list with 3 nodes
What is the printed state of the linked list after reversing it iteratively?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
};
fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut prev = None;
while let Some(mut current) = head {
head = current.next.take();
current.next = prev;
prev = Some(current);
}
prev
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
print!("null\n");
}
fn main() {
let node3 = ListNode { val: 3, next: None };
let node2 = ListNode { val: 2, next: Some(Box::new(node3)) };
let node1 = ListNode { val: 1, next: Some(Box::new(node2)) };
let head = Some(Box::new(node1));
let reversed_head = reverse_list(head);
print_list(&reversed_head);
}Attempts:
2 left
💡 Hint
Trace the pointers step-by-step to see how the links reverse.
✗ Incorrect
The iterative reversal changes the next pointers so the list order reverses. Starting from 1->2->3->null, after reversal it becomes 3->2->1->null.
❓ Predict Output
intermediateOutput when reversing an empty linked list
What is the printed output after reversing an empty linked list iteratively?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
};
fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut prev = None;
while let Some(mut current) = head {
head = current.next.take();
current.next = prev;
prev = Some(current);
}
prev
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
print!("null\n");
}
fn main() {
let head: Option<Box<ListNode>> = None;
let reversed_head = reverse_list(head);
print_list(&reversed_head);
}Attempts:
2 left
💡 Hint
Consider what happens when the input list is empty.
✗ Incorrect
Reversing an empty list returns None, so printing it results in just 'null'.
🔧 Debug
advancedIdentify the error in this iterative reversal code
What error will this code cause when reversing a linked list?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
};
fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut prev = None;
while let Some(current) = head {
head = current.next;
current.next = prev;
prev = Some(current);
}
prev
}Attempts:
2 left
💡 Hint
Check how ownership and moves are handled in Rust when accessing current.next.
✗ Incorrect
The code tries to use current.next after moving current out of head without taking ownership properly, causing a use of moved value error.
❓ Predict Output
advancedFinal list after reversing a 5-node linked list
What is the printed linked list after reversing this 5-node list iteratively?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
};
fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut prev = None;
while let Some(mut current) = head {
head = current.next.take();
current.next = prev;
prev = Some(current);
}
prev
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
print!("null\n");
}
fn main() {
let mut head = None;
for i in (1..=5).rev() {
head = Some(Box::new(ListNode { val: i, next: head }));
}
let reversed_head = reverse_list(head);
print_list(&reversed_head);
}Attempts:
2 left
💡 Hint
Check how the list is constructed before reversal and what reversal does.
✗ Incorrect
The list is constructed as 1->2->3->4->5->null using the reversed iterator. After reversal, it becomes 5->4->3->2->1->null.
🧠 Conceptual
expertWhy is iterative reversal preferred over recursive for large linked lists?
Which reason best explains why iterative reversal is preferred over recursive reversal for very large singly linked lists?
Attempts:
2 left
💡 Hint
Think about memory usage and call stack limits.
✗ Incorrect
Recursive reversal uses call stack frames proportional to list size, risking stack overflow. Iterative reversal uses a few pointers and constant memory.
