Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateWhat is the output of this Circular Singly Linked List after insertions?
Consider a circular singly linked list where we insert nodes with values 10, 20, and 30 in that order. What is the printed state of the list after these insertions?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularSinglyLinkedList {
head: Option<Box<Node>>,
}
impl CircularSinglyLinkedList {
fn new() -> Self {
CircularSinglyLinkedList { head: None }
}
fn insert(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node.clone());
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}
fn print_list(&self) {
if self.head.is_none() {
println!("List is empty");
return;
}
let mut temp = self.head.as_ref();
loop {
print!("{} -> ", temp.unwrap().data);
temp = temp.unwrap().next.as_ref();
if temp.unwrap().data == self.head.as_ref().unwrap().data {
break;
}
}
println!("(head)");
}
}
fn main() {
let mut list = CircularSinglyLinkedList::new();
list.insert(10);
list.insert(20);
list.insert(30);
list.print_list();
}Attempts:
2 left
š” Hint
Remember that new nodes are inserted at the head, and the list is circular.
ā Incorrect
Each new node is inserted at the head, so the last inserted node becomes the new head. The list is circular, so after the last node, it points back to the head.
š§ Conceptual
intermediateHow does a circular singly linked list differ from a regular singly linked list?
Choose the correct statement that describes the main difference between a circular singly linked list and a regular singly linked list.
Attempts:
2 left
š” Hint
Think about what makes the list circular.
ā Incorrect
The key difference is that the last node's next pointer points back to the head, creating a loop.
š§ Debug
advancedWhat error occurs when running this circular singly linked list insertion code?
This Rust code attempts to insert nodes into a circular singly linked list but causes a compile-time error. What is the error?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularSinglyLinkedList {
head: Option<Box<Node>>,
}
impl CircularSinglyLinkedList {
fn insert(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node);
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}
}Attempts:
2 left
š” Hint
Look at how new_node is assigned to its own next field.
ā Incorrect
Assigning new_node.next = Some(new_node) tries to move new_node into itself, which is not allowed and causes a compile-time error.
š Syntax
advancedWhich option correctly implements insertion at the end of a circular singly linked list in Rust?
Select the code snippet that correctly inserts a new node at the end of a circular singly linked list.
Attempts:
2 left
š” Hint
Remember the last node's next must point to head to keep the list circular.
ā Incorrect
Option A correctly sets new_node.next to head and updates the last node's next to new_node, preserving circularity.
š Application
expertWhat is the time complexity of searching for a value in a circular singly linked list of n nodes?
Assuming no additional data structures, what is the time complexity to find a node with a specific value in a circular singly linked list with n nodes?
Attempts:
2 left
š” Hint
Think about how you find a value in a linked list without extra indexing.
ā Incorrect
You must check nodes one by one until you find the value or return to the head, so time is linear in n.
