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 node at position 2?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null. After deleting the node at position 2 (0-based index), what will be the printed list?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
if pos == 0 {
return head.and_then(|node| node.next);
}
let mut current = &mut head;
for _ in 0..pos-1 {
if let Some(ref mut node) = current {
current = &mut node.next;
} else {
return head;
}
}
if let Some(ref mut node) = current {
if let Some(next_node) = node.next.take() {
node.next = next_node.next;
}
}
head
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("null");
}
fn main() {
let node4 = Box::new(Node { data: 40, next: None });
let node3 = Box::new(Node { data: 30, next: Some(node4) });
let node2 = Box::new(Node { data: 20, next: Some(node3) });
let node1 = Box::new(Node { data: 10, next: Some(node2) });
let head = Some(node1);
let new_head = delete_at_position(head, 2);
print_list(&new_head);
}Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list (0-based). Deleting it removes the node with value 30.
✗ Incorrect
Deleting the node at position 2 removes the node with value 30. The list links 20 directly to 40, resulting in 10 -> 20 -> 40 -> null.
❓ Predict Output
intermediateWhat is the output after deleting the head node?
Given a linked list 5 -> 15 -> 25 -> null, what will be the list after deleting the node at position 0?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
if pos == 0 {
return head.and_then(|node| node.next);
}
let mut current = &mut head;
for _ in 0..pos-1 {
if let Some(ref mut node) = current {
current = &mut node.next;
} else {
return head;
}
}
if let Some(ref mut node) = current {
if let Some(next_node) = node.next.take() {
node.next = next_node.next;
}
}
head
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("null");
}
fn main() {
let node3 = Box::new(Node { data: 25, next: None });
let node2 = Box::new(Node { data: 15, next: Some(node3) });
let node1 = Box::new(Node { data: 5, next: Some(node2) });
let head = Some(node1);
let new_head = delete_at_position(head, 0);
print_list(&new_head);
}Attempts:
2 left
💡 Hint
Deleting position 0 removes the head node.
✗ Incorrect
Removing the head node (value 5) makes the next node (value 15) the new head. The list becomes 15 -> 25 -> null.
🔧 Debug
advancedWhat error occurs when deleting a node at an out-of-range position?
Given the linked list 1 -> 2 -> 3 -> null, what happens if we try to delete the node at position 5?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
if pos == 0 {
return head.and_then(|node| node.next);
}
let mut current = &mut head;
for _ in 0..pos-1 {
if let Some(ref mut node) = current {
current = &mut node.next;
} else {
return head;
}
}
if let Some(ref mut node) = current {
if let Some(next_node) = node.next.take() {
node.next = next_node.next;
}
}
head
}
fn main() {
let node3 = Box::new(Node { data: 3, next: None });
let node2 = Box::new(Node { data: 2, next: Some(node3) });
let node1 = Box::new(Node { data: 1, next: Some(node2) });
let head = Some(node1);
let new_head = delete_at_position(head, 5);
// No print to focus on error
}Attempts:
2 left
💡 Hint
Check how the code handles positions beyond list length.
✗ Incorrect
The function returns the original list unchanged if the position is out of range. No panic or error occurs.
❓ Predict Output
advancedWhat is the output after deleting the last node?
Given the linked list 7 -> 14 -> 21 -> 28 -> null, what will be the list after deleting the node at position 3?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
if pos == 0 {
return head.and_then(|node| node.next);
}
let mut current = &mut head;
for _ in 0..pos-1 {
if let Some(ref mut node) = current {
current = &mut node.next;
} else {
return head;
}
}
if let Some(ref mut node) = current {
if let Some(next_node) = node.next.take() {
node.next = next_node.next;
}
}
head
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("null");
}
fn main() {
let node4 = Box::new(Node { data: 28, next: None });
let node3 = Box::new(Node { data: 21, next: Some(node4) });
let node2 = Box::new(Node { data: 14, next: Some(node3) });
let node1 = Box::new(Node { data: 7, next: Some(node2) });
let head = Some(node1);
let new_head = delete_at_position(head, 3);
print_list(&new_head);
}Attempts:
2 left
💡 Hint
Deleting position 3 removes the last node.
✗ Incorrect
Removing the last node (value 28) leaves the list as 7 -> 14 -> 21 -> null.
🧠 Conceptual
expertWhat is the time complexity of deleting a node at a specific position in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity to delete a node at position k (0-based index)?
Attempts:
2 left
💡 Hint
Think about how you reach the node before the one to delete in a singly linked list.
✗ Incorrect
In a singly linked list, to delete a node at position k, you must traverse from the head to the node before position k, which takes O(k) time.
