Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a node from circular linked list
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(data: i32) -> Self {
Node { data, next: None }
}
}
fn delete_node(head: &mut Option<Box<Node>>, key: i32) {
if head.is_none() {
return;
}
let mut curr = head.as_mut().unwrap();
let mut prev: Option<&mut Box<Node>> = None;
loop {
if curr.data == key {
if let Some(p) = prev {
p.next = curr.next.take();
} else {
// Deleting head node
if curr.next.is_none() {
*head = None;
} else {
let mut tail = curr.next.as_mut().unwrap();
while tail.next.is_some() && tail.next.as_ref().unwrap().data != curr.data {
tail = tail.next.as_mut().unwrap();
}
*head = curr.next.take();
tail.next = head.clone();
}
}
break;
}
if curr.next.is_none() {
break;
}
prev = Some(curr);
curr = curr.next.as_mut().unwrap();
}
}
fn print_list(head: &Option<Box<Node>>) {
if head.is_none() {
println!("null");
return;
}
let mut curr = head.as_ref();
let start = curr.unwrap().data;
loop {
print!("{} -> ", curr.unwrap().data);
curr = curr.unwrap().next.as_ref();
if curr.is_none() || curr.unwrap().data == start {
break;
}
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(Node::new(1)));
head.as_mut().unwrap().next = Some(Box::new(Node::new(2)));
head.as_mut().unwrap().next.as_mut().unwrap().next = Some(Box::new(Node::new(3)));
head.as_mut().unwrap().next.as_mut().unwrap().next.as_mut().unwrap().next = head.clone();
delete_node(&mut head, 3);
print_list(&head);
}Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
After deleting node 3, the list contains nodes 1 and 2 linked circularly. The print function stops after printing 2 because the next node is the start node 1 again.
❓ Predict Output
intermediateOutput after deleting the head node in circular linked list
What is the printed state of the circular linked list after deleting the node with value 1 (head node)?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(data: i32) -> Self {
Node { data, next: None }
}
}
fn delete_node(head: &mut Option<Box<Node>>, key: i32) {
if head.is_none() {
return;
}
let mut curr = head.as_mut().unwrap();
let mut prev: Option<&mut Box<Node>> = None;
loop {
if curr.data == key {
if let Some(p) = prev {
p.next = curr.next.take();
} else {
// Deleting head node
if curr.next.is_none() {
*head = None;
} else {
let mut tail = curr.next.as_mut().unwrap();
while tail.next.is_some() && tail.next.as_ref().unwrap().data != curr.data {
tail = tail.next.as_mut().unwrap();
}
*head = curr.next.take();
tail.next = head.clone();
}
}
break;
}
if curr.next.is_none() {
break;
}
prev = Some(curr);
curr = curr.next.as_mut().unwrap();
}
}
fn print_list(head: &Option<Box<Node>>) {
if head.is_none() {
println!("null");
return;
}
let mut curr = head.as_ref();
let start = curr.unwrap().data;
loop {
print!("{} -> ", curr.unwrap().data);
curr = curr.unwrap().next.as_ref();
if curr.is_none() || curr.unwrap().data == start {
break;
}
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(Node::new(1)));
head.as_mut().unwrap().next = Some(Box::new(Node::new(2)));
head.as_mut().unwrap().next.as_mut().unwrap().next = Some(Box::new(Node::new(3)));
head.as_mut().unwrap().next.as_mut().unwrap().next.as_mut().unwrap().next = head.clone();
delete_node(&mut head, 1);
print_list(&head);
}Attempts:
2 left
💡 Hint
Deleting the head means the new head is the next node.
✗ Incorrect
After deleting node 1, the head moves to node 2. The circular link is updated so 3 points back to 2.
🔧 Debug
advancedIdentify the error in circular linked list deletion code
What error will this Rust code produce when trying to delete a node from a circular linked list?
DSA Rust
fn delete_node(head: &mut Option<Box<Node>>, key: i32) {
let mut curr = head.as_mut().unwrap();
while curr.data != key {
curr = curr.next.as_mut().unwrap();
}
curr = curr.next.take();
}Attempts:
2 left
💡 Hint
Consider what happens if the key is not in the list.
✗ Incorrect
If the key is not found, unwrap on None will panic causing runtime error.
🧠 Conceptual
advancedWhy is special handling needed when deleting the head node in circular linked list?
Why must we handle deletion of the head node differently in a circular linked list?
Attempts:
2 left
💡 Hint
Think about the circular link from tail to head.
✗ Incorrect
When deleting head, tail's next pointer must point to new head to maintain circularity.
🚀 Application
expertResulting list after multiple deletions in circular linked list
Given a circular linked list with nodes 1 -> 2 -> 3 -> 4 -> (back to 1), what is the printed list after deleting nodes 2 and 4 in order?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(data: i32) -> Self {
Node { data, next: None }
}
}
fn delete_node(head: &mut Option<Box<Node>>, key: i32) {
if head.is_none() {
return;
}
let mut curr = head.as_mut().unwrap();
let mut prev: Option<&mut Box<Node>> = None;
loop {
if curr.data == key {
if let Some(p) = prev {
p.next = curr.next.take();
} else {
if curr.next.is_none() {
*head = None;
} else {
let mut tail = curr.next.as_mut().unwrap();
while tail.next.is_some() && tail.next.as_ref().unwrap().data != curr.data {
tail = tail.next.as_mut().unwrap();
}
*head = curr.next.take();
tail.next = head.clone();
}
}
break;
}
if curr.next.is_none() {
break;
}
prev = Some(curr);
curr = curr.next.as_mut().unwrap();
}
}
fn print_list(head: &Option<Box<Node>>) {
if head.is_none() {
println!("null");
return;
}
let mut curr = head.as_ref();
let start = curr.unwrap().data;
loop {
print!("{} -> ", curr.unwrap().data);
curr = curr.unwrap().next.as_ref();
if curr.is_none() || curr.unwrap().data == start {
break;
}
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(Node::new(1)));
head.as_mut().unwrap().next = Some(Box::new(Node::new(2)));
head.as_mut().unwrap().next.as_mut().unwrap().next = Some(Box::new(Node::new(3)));
head.as_mut().unwrap().next.as_mut().unwrap().next.as_mut().unwrap().next = Some(Box::new(Node::new(4)));
head.as_mut().unwrap().next.as_mut().unwrap().next.as_mut().unwrap().next.as_mut().unwrap().next = head.clone();
delete_node(&mut head, 2);
delete_node(&mut head, 4);
print_list(&head);
}Attempts:
2 left
💡 Hint
Delete nodes one by one and update links carefully.
✗ Incorrect
After deleting 2 and 4, only nodes 1 and 3 remain linked circularly.
