Complete the code to check if the linked list has no next node (end).
if current.next.is_[1]() { return false; }
The is_none() method checks if the Option is None, meaning no next node exists, so the list is not circular.
Complete the code to move the slow pointer one step forward.
slow = slow.next.as_ref().map(|node| &**[1]).unwrap();The closure receives node as the reference to the next node, so we dereference it to get the node itself.
Fix the error in the condition to detect if slow and fast pointers meet.
if std::ptr::eq(slow, [1]) { return true; }
We compare the slow pointer with the fast pointer to detect a cycle.
Fill both blanks to advance the fast pointer two steps safely.
fast = fast.next.as_ref().and_then(|node| node.next.as_ref().map(|[1]| &**[2])).unwrap_or(fast);
The first closure argument is named node, and the second closure argument is named next_node to access the second next node.
Fill all three blanks to complete the function that detects if a linked list is circular.
pub fn is_circular(head: Option<&Box<Node>>) -> bool {
let mut slow = head.unwrap();
let mut fast = head.unwrap();
while fast.next.is_some() && fast.next.as_ref().unwrap().next.is_some() {
slow = slow.next.as_ref().map(|[1]| &**[2]).unwrap();
fast = fast.next.as_ref().and_then(|node| node.next.as_ref().map(|[3]| &**[3])).unwrap();
if std::ptr::eq(slow, fast) {
return true;
}
}
false
}The closures use node as the argument name to access the next nodes. The fast pointer is advanced by two steps and compared with slow.
