Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Rust
let new_node = Box::new(Node { data: [1], next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of the new value.
Setting data to None which is invalid.
✗ Incorrect
We create a new node with the data field set to the given value.
2fill in blank
mediumComplete the code to handle the empty list case by pointing new_node.next to itself.
DSA Rust
if head.is_none() { new_node.next = Some(new_node[1]); return Some(new_node); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap() which consumes the Box and causes errors.
Using as_ref() which returns a reference, not a Box.
✗ Incorrect
We clone new_node to create a reference to itself for the next pointer.
3fill in blank
hardFix the error in the loop condition to find the last node in the circular list.
DSA Rust
let mut current = head.as_ref().unwrap(); while current.next.as_ref().unwrap() != head.as_ref().unwrap() [1] { current = current.next.as_ref().unwrap(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes the loop to stop immediately.
Using '<' or '>' is invalid for pointer comparison.
✗ Incorrect
We loop until we find the node whose next points back to head, so the condition is '!='.
4fill in blank
hardFill both blanks to update pointers to insert new_node at the beginning.
DSA Rust
new_node.next = Some(head.as_ref().unwrap().clone()); current.next = Some([1]); return Some([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Returning head instead of new_node.
Setting current.next to head instead of new_node.
✗ Incorrect
We set current.next to new_node and return new_node as the new head.
5fill in blank
hardFill all three blanks to complete the insert_at_beginning function for circular linked list.
DSA Rust
fn insert_at_beginning(head: Option<Box<Node>>, value: i32) -> Option<Box<Node>> {
let mut new_node = Box::new(Node { data: [1], next: None });
if head.is_none() {
new_node.next = Some(new_node[2]);
return Some(new_node);
}
let mut current = head.as_ref().unwrap();
while current.next.as_ref().unwrap() != head.as_ref().unwrap() {
current = current.next.as_ref().unwrap();
}
new_node.next = Some(head.as_ref().unwrap().clone());
current.next = Some([3]);
Some(new_node)
}Attempts:
3 left
💡 Hint
Common Mistakes
Not cloning new_node causing ownership errors.
Setting next to head instead of new_node in empty case.
✗ Incorrect
We set new_node data to value, next points to itself by cloning, and current.next points to new_node clone.
