Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the same value as the current node.
DSA Rust
let new_node = Node::new([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next or current.random instead of current.val
Using None which creates an empty node
✗ Incorrect
We need to copy the value of the current node to create the new node.
2fill in blank
mediumComplete the code to link the new node's next pointer to the next node in the original list.
DSA Rust
new_node.next = current.[1].clone();Attempts:
3 left
💡 Hint
Common Mistakes
Using random instead of next
Using val which is a value, not a pointer
✗ Incorrect
The next pointer of the new node should point to the next node of the current node.
3fill in blank
hardFix the error in assigning the random pointer of the cloned node.
DSA Rust
cloned_node.random = map.get(¤t.random).[1]().cloned();Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap which can panic if None
Using clone directly on Option<&Node>
✗ Incorrect
We use as_ref() to convert Option<&Node> to Option before cloning.
4fill in blank
hardFill both blanks to correctly insert cloned nodes between original nodes.
DSA Rust
while let Some(current) = [1] { let cloned = Node::new(current.val); cloned.next = current.next.clone(); current.next = Some(cloned); [2] = current.next.as_ref().unwrap().next.clone(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using head.next instead of current for iteration
Moving current incorrectly causing infinite loop
✗ Incorrect
We start with current = head and move current to current.next after insertion.
5fill in blank
hardFill all three blanks to correctly separate the cloned list from the original list.
DSA Rust
let mut original = head.clone(); let mut clone_head = head.as_ref().unwrap().next.clone(); let mut clone = clone_head.clone(); while let Some(orig_node) = original { orig_node.next = orig_node.next.as_ref().unwrap().next.clone(); clone.as_mut().unwrap().next = clone.as_ref().unwrap().next.as_ref().map(|node| node.clone()); original = orig_node.next.clone(); clone = clone.as_ref().unwrap().next.clone(); } return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning original or head instead of clone_head
Returning clone which may be None at end
✗ Incorrect
After separation, we return the head of the cloned list.
