📖 Scenario: You are managing a circular linked list that represents a round-robin queue of players in a game. Each player has a unique number. You want to add a new player at the beginning of the queue so they get the first turn.
🎯 Goal: Build a circular linked list and write code to insert a new node at the beginning, maintaining the circular structure.
📋 What You'll Learn
Create a struct called Node with fields data: i32 and next: Option<Rc<RefCell<Node>>>
Create a variable called head that points to the start of the circular linked list
Create a function called insert_at_beginning that takes head: Option<Rc<RefCell<Node>>> and data: i32 and returns Option<Rc<RefCell<Node>>>
Print the circular linked list starting from head after insertion
💡 Why This Matters
🌍 Real World
Circular linked lists are used in real-time gaming, operating systems for task scheduling, and network token passing where the order repeats indefinitely.
💼 Career
Understanding circular linked lists helps in roles involving system programming, embedded systems, and game development where efficient cyclic data structures are needed.
Progress0 / 4 steps
1
Create initial circular linked list with three nodes
Create a struct called Node with fields data: i32 and next: Option<Rc<RefCell<Node>>>. Then create three nodes with data 10, 20, and 30 linked in a circular way. Assign the first node to a variable called head.
DSA Rust
Hint
Remember to link the last node back to the first node to make the list circular. Use Rc and RefCell for safe circular references.
2
Add a variable for new data to insert
Create a variable called new_data and set it to 5. This will be the data for the new node to insert at the beginning.
DSA Rust
Hint
Just create a variable new_data and assign 5 to it.
3
Write function to insert new node at beginning
Write a function called insert_at_beginning that takes head: Option<Rc<RefCell<Node>>> and data: i32 and returns Option<Rc<RefCell<Node>>>. This function should insert a new node with data at the beginning of the circular linked list and return the new head.
DSA Rust
Hint
Traverse to find the tail (node whose next.data == old head data). Update tail.next to new_node, then new_node.next to old head. Use .clone() for Rc.
4
Print circular linked list after insertion
Call insert_at_beginning with head and new_data. Then print the circular linked list starting from the new head. Print each node's data followed by -> and end with null. Traverse exactly the number of nodes in the list to avoid infinite loop.
DSA Rust
Hint
Call insert_at_beginning with head and new_data. Traverse exactly 4 nodes using current.borrow().data and current = current.borrow().next.as_ref().unwrap().clone(). End with null.
Practice
(1/5)
1. What happens when you insert a new node at the beginning of a circular linked list?
easy
A. The new node is added at the end and does not affect the head.
B. The new node replaces the last node in the list.
C. The list breaks and becomes a linear linked list.
D. The new node becomes the head and points to the old head, maintaining the circular link.
Solution
Step 1: Understand circular linked list structure
In a circular linked list, the last node points back to the head node, forming a circle.
Step 2: Insert new node at beginning
The new node is linked to the current head, and the last node's next pointer is updated to this new node, making it the new head.
Final Answer:
The new node becomes the head and points to the old head, maintaining the circular link. -> Option D
Quick Check:
New head points to old head, circle intact [OK]
Hint: New head points to old head; last node links to new head [OK]
Common Mistakes:
Forgetting to update last node's next pointer
Breaking the circular link accidentally
Replacing the last node instead of adding new head
2. Which Rust code snippet correctly creates a new node and inserts it at the beginning of a circular linked list?
easy
A. let new_node = Node { data: val, next: head.clone() }; last.next = Some(new_node.clone()); head = Some(new_node);
B. let new_node = Node { data: val, next: None }; head = Some(new_node);
C. let new_node = Node { data: val, next: last.clone() }; head = Some(new_node);
D. let new_node = Node { data: val, next: head.clone() }; head = Some(new_node);
Solution
Step 1: Create new node pointing to current head
The new node's next pointer should point to the current head to maintain the list.
Step 2: Update last node's next to new node and update head
Since it's circular, last node's next must point to new node, then head is updated to new node.
Final Answer:
let new_node = Node { data: val, next: head.clone() }; last.next = Some(new_node.clone()); head = Some(new_node); -> Option A
Quick Check:
New node next = head, last next = new node, head updated [OK]
Hint: New node next = head; last.next updated; head = new node [OK]
Common Mistakes:
Not updating last node's next pointer
Setting new node's next to None
Not cloning nodes properly causing ownership errors
3. Given the following Rust code for inserting at the beginning of a circular linked list, what will be the printed list after insertion?
Insert 10 first: list is 10 -> 10 (single node points to itself). Insert 20: new node 20 points to 10, last node (10) points to 20, head updated to 20. Insert 30: new node 30 points to 20, last node (10) points to 30, head updated to 30.
Step 2: Print list starting from head
Starting at 30, next is 20, then 10, then back to 30 (stop). So printed: 30 -> 20 -> 10 -> null.
Final Answer:
30 -> 20 -> 10 -> null -> Option B
Quick Check:
New nodes inserted at head, order reversed [OK]
Hint: New insertions become head, list order reverses [OK]
Common Mistakes:
Confusing order of nodes after insertion
Not updating last node's next pointer correctly
Incorrect loop condition in print function
4. Identify the bug in this Rust function that inserts a node at the beginning of a circular linked list:
A. head.take() removes the list causing loss of nodes.
B. The while loop condition never terminates causing infinite loop.
C. Cloning new_node causes infinite recursion and stack overflow.
D. temp.next is not updated correctly to point to new_node.
Solution
Step 1: Analyze cloning of new_node
Cloning new_node inside new_node.next = Some(new_node.clone()) causes recursive cloning because new_node contains next which contains new_node again, leading to infinite recursion.
Step 2: Understand Rust cloning behavior
Rust's clone on a Box<Node> will recursively clone the entire structure, so cloning a node that points to itself causes stack overflow.
Final Answer:
Cloning new_node causes infinite recursion and stack overflow. -> Option C
Hint: Avoid cloning self-referential nodes to prevent recursion [OK]
Common Mistakes:
Ignoring cloning impact on self-referential structures
Assuming head.take() deletes nodes permanently
Misreading loop condition as infinite
5. You want to insert a new node at the beginning of a circular linked list that may be empty or non-empty. Which approach correctly handles both cases in Rust without cloning self-referential nodes?
fn insert_beginning(head: &mut Option, val: i32) {
let mut new_node = Box::new(Node { data: val, next: None });
if head.is_none() {
// Case 1: Empty list
new_node.next = Some(new_node); // Problem here
*head = Some(new_node);
return;
}
// Case 2: Non-empty list
let mut temp = head.as_mut().unwrap();
while temp.next.as_ref().unwrap().as_ref() != head.as_ref().unwrap().as_ref() {
temp = temp.next.as_mut().unwrap();
}
new_node.next = head.take();
temp.next = Some(new_node);
*head = temp.next.clone();
}
What is the correct fix for Case 1 to avoid cloning and infinite recursion?
hard
A. Use a raw pointer or Rc to create self-reference safely.
B. Set new_node.next = None and update last node later.
C. Create a temporary node and assign new_node.next to it.
D. Use unsafe code to manually set new_node.next to itself.
Solution
Step 1: Understand self-referential node problem
Assigning new_node.next = Some(new_node) tries to move new_node into itself, which is invalid and causes infinite recursion if cloned.
Step 2: Use Rc or raw pointers
Rust's safe way to handle self-references is using Rc (reference counting) and RefCell (interior mutability) to create cyclic references without cloning issues.
Step 3: Avoid unsafe code unless necessary
Using unsafe code is error-prone; Rc<RefCell> is the recommended safe approach.
Final Answer:
Use a raw pointer or Rc to create self-reference safely. -> Option A
Quick Check:
Safe self-reference needs Rc/RefCell or raw pointers [OK]
Hint: Use Rc<RefCell> for safe self-referential nodes [OK]
Common Mistakes:
Assigning node to itself directly causing move errors
Using None breaks circular property
Using unsafe code without understanding risks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.