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 a middle value from doubly linked list
What is the printed state of the doubly linked list after deleting the value 3?
DSA Rust
struct Node {
value: 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, val: i32) {
let mut new_node = Box::new(Node { value: val, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
new_node.prev = Some(&mut **current as *mut Node);
current.next = Some(new_node);
}
}
}
fn delete_by_value(&mut self, val: i32) {
let mut current = self.head.as_mut().map(|node| &mut **node as *mut Node);
unsafe {
while let Some(node_ptr) = current {
let node = &mut *node_ptr;
if node.value == val {
if let Some(prev_ptr) = node.prev {
let prev = &mut *prev_ptr;
prev.next = node.next.take();
if let Some(ref mut next_node) = prev.next {
next_node.prev = Some(prev_ptr);
}
} else {
self.head = node.next.take();
if let Some(ref mut head_node) = self.head {
head_node.prev = None;
}
}
break;
}
current = node.next.as_mut().map(|next_node| &mut **next_node as *mut Node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.value);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = DoublyLinkedList::new();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.delete_by_value(3);
list.print_list();
}Attempts:
2 left
💡 Hint
Focus on how the node with value 3 is removed and how links are updated.
✗ Incorrect
The node with value 3 is removed, so the list links 2 directly to 4. The printed list shows 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateOutput after deleting head value in doubly linked list
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA Rust
struct Node {
value: 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, val: i32) {
let mut new_node = Box::new(Node { value: val, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
new_node.prev = Some(&mut **current as *mut Node);
current.next = Some(new_node);
}
}
}
fn delete_by_value(&mut self, val: i32) {
let mut current = self.head.as_mut().map(|node| &mut **node as *mut Node);
unsafe {
while let Some(node_ptr) = current {
let node = &mut *node_ptr;
if node.value == val {
if let Some(prev_ptr) = node.prev {
let prev = &mut *prev_ptr;
prev.next = node.next.take();
if let Some(ref mut next_node) = prev.next {
next_node.prev = Some(prev_ptr);
}
} else {
self.head = node.next.take();
if let Some(ref mut head_node) = self.head {
head_node.prev = None;
}
}
break;
}
current = node.next.as_mut().map(|next_node| &mut **next_node as *mut Node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.value);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = DoublyLinkedList::new();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.delete_by_value(1);
list.print_list();
}Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
✗ Incorrect
After deleting the head node with value 1, the new head is the node with value 2. The list prints 2 -> 3 -> 4 -> null.
🔧 Debug
advancedIdentify the bug causing incorrect deletion in doubly linked list
Given the delete_by_value function below, what is the bug that causes the list to become corrupted after deleting a node?
DSA Rust
fn delete_by_value(&mut self, val: i32) {
let mut current = self.head.as_mut();
while let Some(node) = current {
if node.value == val {
if let Some(prev) = node.prev.as_mut() {
prev.next = node.next.take();
} else {
self.head = node.next.take();
}
break;
}
current = node.next.as_mut();
}
}Attempts:
2 left
💡 Hint
Think about the links on both sides of the deleted node.
✗ Incorrect
After removing the node, the next node's prev pointer must be updated to point to the previous node. Missing this causes list corruption.
❓ Predict Output
advancedOutput after deleting a non-existent value in doubly linked list
What is the printed state of the doubly linked list after attempting to delete the value 5 which does not exist in the list?
DSA Rust
struct Node {
value: 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, val: i32) {
let mut new_node = Box::new(Node { value: val, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
new_node.prev = Some(&mut **current as *mut Node);
current.next = Some(new_node);
}
}
}
fn delete_by_value(&mut self, val: i32) {
let mut current = self.head.as_mut().map(|node| &mut **node as *mut Node);
unsafe {
while let Some(node_ptr) = current {
let node = &mut *node_ptr;
if node.value == val {
if let Some(prev_ptr) = node.prev {
let prev = &mut *prev_ptr;
prev.next = node.next.take();
if let Some(ref mut next_node) = prev.next {
next_node.prev = Some(prev_ptr);
}
} else {
self.head = node.next.take();
if let Some(ref mut head_node) = self.head {
head_node.prev = None;
}
}
break;
}
current = node.next.as_mut().map(|next_node| &mut **next_node as *mut Node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.value);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = DoublyLinkedList::new();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.delete_by_value(5);
list.print_list();
}Attempts:
2 left
💡 Hint
Deleting a value not in the list should not change the list.
✗ Incorrect
Since value 5 is not found, the list remains unchanged and prints 1 -> 2 -> 3 -> 4 -> null.
❓ Predict Output
expertOutput after deleting all occurrences of a value in doubly linked list
What is the printed state of the doubly linked list after deleting all nodes with value 2?
DSA Rust
struct Node {
value: 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, val: i32) {
let mut new_node = Box::new(Node { value: val, prev: None, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut current) => {
while let Some(ref mut next) = current.next {
current = next;
}
new_node.prev = Some(&mut **current as *mut Node);
current.next = Some(new_node);
}
}
}
fn delete_all_by_value(&mut self, val: i32) {
let mut current = self.head.as_mut().map(|node| &mut **node as *mut Node);
unsafe {
while let Some(node_ptr) = current {
let node = &mut *node_ptr;
if node.value == val {
if let Some(prev_ptr) = node.prev {
let prev = &mut *prev_ptr;
prev.next = node.next.take();
if let Some(ref mut next_node) = prev.next {
next_node.prev = Some(prev_ptr);
}
current = prev.next.as_mut().map(|next_node| &mut **next_node as *mut Node);
} else {
self.head = node.next.take();
if let Some(ref mut head_node) = self.head {
head_node.prev = None;
}
current = self.head.as_mut().map(|node| &mut **node as *mut Node);
}
} else {
current = node.next.as_mut().map(|next_node| &mut **next_node as *mut Node);
}
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.value);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = DoublyLinkedList::new();
list.append(1);
list.append(2);
list.append(3);
list.append(2);
list.append(4);
list.delete_all_by_value(2);
list.print_list();
}Attempts:
2 left
💡 Hint
All nodes with value 2 should be removed, including multiple occurrences.
✗ Incorrect
Both nodes with value 2 are deleted, leaving 1 -> 3 -> 4 -> null.
