Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the first node?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> null. After deleting the node at the beginning, what will be the printed list?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn delete_at_beginning(head: Option<Box<Node>>) -> Option<Box<Node>> {
match head {
None => None,
Some(node) => node.next,
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
fn main() {
let third = Box::new(Node { data: 30, next: None });
let second = Box::new(Node { data: 20, next: Some(third) });
let first = Box::new(Node { data: 10, next: Some(second) });
let head = Some(first);
let head = delete_at_beginning(head);
print_list(&head);
}Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
✗ Incorrect
The function removes the first node by returning the next node as the new head. So the list starts from 20 now.
❓ Predict Output
intermediateWhat happens if we delete from an empty list?
Given an empty linked list (head is None), what will be the output after calling delete_at_beginning and then printing the list?
DSA Rust
fn main() {
let head: Option<Box<Node>> = None;
let head = delete_at_beginning(head);
print_list(&head);
}Attempts:
2 left
💡 Hint
Deleting from an empty list should keep it empty.
✗ Incorrect
The function returns None when the list is empty, so printing shows 'null'.
🔧 Debug
advancedWhy does this code cause a compile error?
This Rust code tries to delete the first node but fails to compile. What is the cause?
```rust
fn delete_at_beginning(head: Option>) -> Option> {
head.next
}
```
Attempts:
2 left
💡 Hint
Option is an enum, not a struct with fields.
✗ Incorrect
head is an Option, so you cannot access .next directly without unwrapping the Option first.
❓ Predict Output
advancedWhat is the output after deleting the first node twice?
Given a linked list 5 -> 15 -> 25 -> null, after deleting the first node two times, what will be the printed list?
DSA Rust
fn main() {
let third = Box::new(Node { data: 25, next: None });
let second = Box::new(Node { data: 15, next: Some(third) });
let first = Box::new(Node { data: 5, next: Some(second) });
let mut head = Some(first);
head = delete_at_beginning(head);
head = delete_at_beginning(head);
print_list(&head);
}Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
✗ Incorrect
First deletion removes 5, second removes 15, so list starts at 25.
🧠 Conceptual
expertWhy is deleting the first node in a singly linked list efficient?
Which reason best explains why deleting the first node in a singly linked list is faster than deleting a node in the middle?
Attempts:
2 left
💡 Hint
Think about how much work is needed to remove the first node.
✗ Incorrect
Deleting the first node just moves the head pointer, no need to find previous nodes.
