Challenge - 5 Problems
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting the last node in a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn delete_end(&mut self) {
if self.head.is_none() {
return;
}
if self.head.as_ref().unwrap().next.is_none() {
self.head = None;
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.append(10);
list.append(20);
list.append(30);
list.delete_end();
list.print_list();
}Attempts:
2 left
💡 Hint
Deleting the last node removes the node with value 30.
✗ Incorrect
The list initially has nodes 10 -> 20 -> 30 -> null. After deleting the last node (30), the list becomes 10 -> 20 -> null.
❓ Predict Output
intermediateOutput after deleting the last node from a single-node list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn delete_end(&mut self) {
if self.head.is_none() {
return;
}
if self.head.as_ref().unwrap().next.is_none() {
self.head = None;
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.append(100);
list.delete_end();
list.print_list();
}Attempts:
2 left
💡 Hint
Deleting the last node from a single-node list results in an empty list.
✗ Incorrect
The list initially has one node 100 -> null. After deleting the last node, the list becomes empty, so printing shows null.
🔧 Debug
advancedIdentify the error in deleting the last node
What error will this code produce when deleting the last node from an empty list?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn delete_end(&mut self) {
let mut current = self.head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
}
fn main() {
let mut list = LinkedList { head: None };
list.delete_end();
}Attempts:
2 left
💡 Hint
The code calls unwrap on None without checking if head is None.
✗ Incorrect
Calling unwrap on None causes a panic at runtime with the message 'called `Option::unwrap()` on a `None` value'.
🧠 Conceptual
advancedWhy is a mutable reference needed to delete the last node?
In Rust, why do we need a mutable reference to traverse and delete the last node in a singly linked list?
Attempts:
2 left
💡 Hint
Deleting the last node requires changing the pointer of the previous node.
✗ Incorrect
To delete the last node, we must set the next pointer of the second last node to None, which requires mutable access.
❓ Predict Output
expertOutput after multiple deletions at the end
What is the printed state of the linked list after deleting the last node three times from the initial list 5 -> 15 -> 25 -> 35 -> 45 -> null?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn delete_end(&mut self) {
if self.head.is_none() {
return;
}
if self.head.as_ref().unwrap().next.is_none() {
self.head = None;
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.data);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.append(5);
list.append(15);
list.append(25);
list.append(35);
list.append(45);
list.delete_end();
list.delete_end();
list.delete_end();
list.print_list();
}Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions, three nodes are removed.
✗ Incorrect
Starting with 5 -> 15 -> 25 -> 35 -> 45 -> null, deleting last node thrice removes 45, 35, and 25, leaving 5 -> 15 -> null.
