Challenge - 5 Problems
Delete Node by Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a middle node in a singly linked list
What is the printed state of the linked list after deleting the node with value 3?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn delete_by_value(head: &mut Option<Box<Node>>, val: i32) {
let mut current = head;
while let Some(ref mut node) = current {
if node.value == val {
*current = node.next.take();
return;
} else {
current = &mut node.next;
}
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(ref node) = current {
print!("{} -> ", node.value);
current = &node.next;
}
println!("null");
}
}
fn main() {
let mut head = Some(Box::new(Node { value: 1, next: Some(Box::new(Node { value: 2, next: Some(Box::new(Node { value: 3, next: Some(Box::new(Node { value: 4, next: None })) })) })) }));
Node::delete_by_value(&mut head, 3);
Node::print_list(&head);
}Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
The node with value 3 is found and removed by changing the previous node's next pointer to skip it. The list then links 2 directly to 4.
❓ Predict Output
intermediateOutput after deleting the head node in a singly linked list
What is the printed state of the linked list after deleting the node with value 1 (the head)?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn delete_by_value(head: &mut Option<Box<Node>>, val: i32) {
let mut current = head;
while let Some(ref mut node) = current {
if node.value == val {
*current = node.next.take();
return;
} else {
current = &mut node.next;
}
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(ref node) = current {
print!("{} -> ", node.value);
current = &node.next;
}
println!("null");
}
}
fn main() {
let mut head = Some(Box::new(Node { value: 1, next: Some(Box::new(Node { value: 2, next: Some(Box::new(Node { value: 3, next: None })) })) }));
Node::delete_by_value(&mut head, 1);
Node::print_list(&head);
}Attempts:
2 left
💡 Hint
Deleting the head means the head pointer changes to the next node.
✗ Incorrect
The head node with value 1 is removed by updating the head pointer to the next node, which has value 2.
🔧 Debug
advancedIdentify the error in this delete by value implementation
What error will this Rust code produce when trying to delete a node by value?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn delete_by_value(head: &mut Option<Box<Node>>, val: i32) {
let mut current = head;
while let Some(node) = current {
if node.value == val {
*current = node.next.take();
return;
} else {
current = &mut node.next;
}
}
}
}Attempts:
2 left
💡 Hint
Look at how `node` is used inside the while loop and whether it is moved or borrowed.
✗ Incorrect
The code tries to move `node` out of a borrowed reference, which Rust forbids. The correct pattern uses `ref mut` to borrow mutably without moving.
❓ Predict Output
advancedOutput after deleting a non-existent value
What is the printed state of the linked list after trying to delete the node with value 10, which does not exist?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn delete_by_value(head: &mut Option<Box<Node>>, val: i32) {
let mut current = head;
while let Some(ref mut node) = current {
if node.value == val {
*current = node.next.take();
return;
} else {
current = &mut node.next;
}
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(ref node) = current {
print!("{} -> ", node.value);
current = &node.next;
}
println!("null");
}
}
fn main() {
let mut head = Some(Box::new(Node { value: 5, next: Some(Box::new(Node { value: 6, next: Some(Box::new(Node { value: 7, next: None })) })) }));
Node::delete_by_value(&mut head, 10);
Node::print_list(&head);
}Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since the value 10 is not in the list, no nodes are removed and the list stays the same.
🧠 Conceptual
expertWhy is using a mutable reference to Option> important in delete by value?
In Rust, why do we use a mutable reference to Option> (like &mut Option>) when deleting a node by value in a singly linked list?
Attempts:
2 left
💡 Hint
Think about how you remove a node without a previous pointer and how Rust ownership rules apply.
✗ Incorrect
Using &mut Option> lets you change the pointer that points to the current node, including the head pointer. This way, you can remove nodes by changing links without needing a separate previous pointer.
