Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of removing 2nd node from end in a 5-node list
What is the printed linked list after removing the 2nd node from the end in the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut first = &dummy;
let mut second = &mut dummy;
for _ in 0..=n {
first = first.next.as_ref().unwrap();
}
while let Some(node) = first.next.as_ref() {
first = node;
second = second.next.as_mut().unwrap();
}
let next = second.next.as_mut().unwrap().next.take();
second.next = next;
dummy.next
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
println!("null");
}
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: Some(Box::new(ListNode { val: 5, next: None })) })) })) })) }));
head = remove_nth_from_end(head, 2);
print_list(&head);
}Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
✗ Incorrect
Removing the 2nd node from the end removes the node with value 4. The resulting list is 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateOutput after removing last node from a 3-node list
What is the printed linked list after removing the 1st node from the end in the list 10 -> 20 -> 30 -> null?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut first = &dummy;
let mut second = &mut dummy;
for _ in 0..=n {
first = first.next.as_ref().unwrap();
}
while let Some(node) = first.next.as_ref() {
first = node;
second = second.next.as_mut().unwrap();
}
let next = second.next.as_mut().unwrap().next.take();
second.next = next;
dummy.next
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(ListNode { val: 10, next: Some(Box::new(ListNode { val: 20, next: Some(Box::new(ListNode { val: 30, next: None })) })) }));
head = remove_nth_from_end(head, 1);
print_list(&head);
}Attempts:
2 left
💡 Hint
Removing the 1st node from the end means removing the last node.
✗ Incorrect
The last node with value 30 is removed, so the list becomes 10 -> 20 -> null.
🔧 Debug
advancedIdentify the error in this Rust code for removing nth node from end
What error does this Rust code produce when trying to remove the 3rd node from the end in a 3-node list?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut first = &dummy;
let mut second = &mut dummy;
for _ in 0..=n {
first = first.next.as_ref().unwrap();
}
while let Some(node) = first.next.as_ref() {
first = node;
second = second.next.as_mut().unwrap();
}
let next = second.next.as_mut().unwrap().next.take();
second.next = next;
dummy.next
}
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: None })) })) }));
head = remove_nth_from_end(head, 3);
}Attempts:
2 left
💡 Hint
Check the for loop range and how many times unwrap is called.
✗ Incorrect
The for loop runs n times but should run n+1 times to avoid unwrap on None. This causes panic at runtime.
🧠 Conceptual
advancedWhy use a dummy node in removing nth node from end?
Why is a dummy node used at the start of the linked list when removing the nth node from the end?
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.
✗ Incorrect
The dummy node acts as a placeholder before the head, making it easier to remove the head node without special checks.
🚀 Application
expertResulting list after removing 4th node from end in 6-node list
Given the list 5 -> 10 -> 15 -> 20 -> 25 -> 30 -> null, what is the list after removing the 4th node from the end?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: head });
let mut first = &dummy;
let mut second = &mut dummy;
for _ in 0..=n {
first = first.next.as_ref().unwrap();
}
while let Some(node) = first.next.as_ref() {
first = node;
second = second.next.as_mut().unwrap();
}
let next = second.next.as_mut().unwrap().next.take();
second.next = next;
dummy.next
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(ListNode { val: 5, next: Some(Box::new(ListNode { val: 10, next: Some(Box::new(ListNode { val: 15, next: Some(Box::new(ListNode { val: 20, next: Some(Box::new(ListNode { val: 25, next: Some(Box::new(ListNode { val: 30, next: None })) })) })) })) })) }));
head = remove_nth_from_end(head, 4);
print_list(&head);
}Attempts:
2 left
💡 Hint
The 4th node from the end is the 3rd node from the start in a 6-node list.
✗ Incorrect
Removing the 4th node from the end removes the node with value 15. The resulting list is 5 -> 10 -> 20 -> 25 -> 30 -> null.
