Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA Rust
struct Node {
data: i32,
prev: Option<Box<Node>>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn append(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next_node) = current.next {
current = next_node;
}
current.next = Some(new_node);
}
}
}
fn delete_from_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_mut().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
fn print_list(&self) {
let mut current = &self.head;
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} -> ", node.data));
current = &node.next;
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.append(1);
dll.append(2);
dll.append(3);
dll.delete_from_end();
dll.print_list();
}Attempts:
2 left
💡 Hint
Think about what happens to the last node and the previous node's next pointer.
✗ Incorrect
Deleting from the end removes the last node (3). The list then ends at node 2, so the printed list is '1 -> 2 -> null'.
❓ Predict Output
intermediateResult after deleting from a single-node doubly linked list
What is the output after deleting the last node from a doubly linked list with only one node?
DSA Rust
struct Node {
data: i32,
prev: Option<Box<Node>>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn append(&mut self, data: i32) {
let new_node = Box::new(Node { data, prev: None, next: None });
self.head = Some(new_node);
}
fn delete_from_end(&mut self) {
if self.head.is_none() {
return;
}
self.head = None;
}
fn print_list(&self) {
let mut current = &self.head;
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} -> ", node.data));
current = &node.next;
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.append(10);
dll.delete_from_end();
dll.print_list();
}Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
After deleting the only node, the list becomes empty, so printing shows 'null'.
🔧 Debug
advancedIdentify the bug in delete_from_end method
What error will this Rust code cause when deleting the last node from a doubly linked list?
DSA Rust
fn delete_from_end(&mut self) {
if self.head.is_none() {
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
if current.prev.is_none() {
self.head = None;
} else {
current.prev = None;
}
}Attempts:
2 left
💡 Hint
Consider mutability and ownership when modifying fields through references.
✗ Incorrect
The code creates a mutable reference `current` from `self.head.as_mut()`, then in the case where `current.prev.is_none()`, it tries to set `self.head = None` while the mutable borrow via `current` is still active. This causes a compilation error due to borrowing rules.
🧠 Conceptual
advancedEffect of deleting last node on doubly linked list pointers
After deleting the last node from a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about what the last node's previous node points to after deletion.
✗ Incorrect
Deleting the last node means the previous node becomes the new end, so its next pointer must be None to indicate the end of the list.
❓ Predict Output
expertOutput after multiple deletions from end of doubly linked list
Given the following operations on a doubly linked list, what is the final printed output?
DSA Rust
struct Node {
data: i32,
prev: Option<*mut Node>,
next: Option<Box<Node>>,
}
struct DoublyLinkedList {
head: Option<Box<Node>>,
}
impl DoublyLinkedList {
fn new() -> Self {
DoublyLinkedList { head: None }
}
fn append(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next_node) = current.next {
current = next_node;
}
new_node.prev = Some(current.as_mut() as *mut Node);
current.next = Some(new_node);
}
}
}
fn delete_from_end(&mut self) {
if self.head.is_none() {
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
unsafe {
if let Some(prev_ptr) = current.prev {
(*prev_ptr).next = None;
} else {
self.head = None;
}
}
}
fn print_list(&self) {
let mut current = &self.head;
let mut result = String::new();
while let Some(node) = current {
result.push_str(&format!("{} -> ", node.data));
current = &node.next;
}
result.push_str("null");
println!("{}", result);
}
}
fn main() {
let mut dll = DoublyLinkedList::new();
dll.append(5);
dll.append(10);
dll.append(15);
dll.delete_from_end();
dll.delete_from_end();
dll.print_list();
}Attempts:
2 left
💡 Hint
Two deletions from end remove last two nodes.
✗ Incorrect
After appending 5, 10, 15, deleting from end twice removes 15 and then 10, leaving only 5 in the list.
