Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the previous and next pointers of the current node.
DSA Rust
let temp = current.[1]; current.[1] = current.next.clone(); current.next = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the 'value' field instead of pointers.
Swapping 'next' with itself instead of 'prev'.
✗ Incorrect
Swapping the 'prev' and 'next' pointers is essential to reverse the links in a doubly linked list.
2fill in blank
mediumComplete the code to move the current pointer to the next node after swapping.
DSA Rust
current = current.[1].clone();Attempts:
3 left
💡 Hint
Common Mistakes
Moving to current.next instead of current.prev after swapping.
Using 'head' or 'tail' which are not node pointers.
✗ Incorrect
After swapping, the original 'next' is now in 'prev', so we move to current.prev to continue reversing.
3fill in blank
hardFix the error in updating the head pointer after reversing the list.
DSA Rust
if let Some(node) = current [1] { head = Some(node); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.unwrap() which causes panic.
Using next pointer which is None at the end.
✗ Incorrect
After the loop, current is None, so we use the last node's prev pointer to set the new head.
4fill in blank
hardFill both blanks to complete the loop that reverses the doubly linked list.
DSA Rust
while let Some(mut current) = node [1] { let temp = current.[2]; current.[2] = current.next.clone(); current.next = temp; node = current.[1].clone(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the wrong pointer inside the loop.
Moving the node pointer incorrectly.
✗ Incorrect
We move forward using 'next' and swap 'prev' pointer inside the loop to reverse links.
5fill in blank
hardFill all three blanks to create a reversed doubly linked list using Rust Option and Rc.
DSA Rust
fn reverse(mut head: Option<Rc<RefCell<Node>>>) -> Option<Rc<RefCell<Node>>> {
let mut node = head.clone();
while let Some(current) = node [1] {
let temp = current.borrow().[2].clone();
current.borrow_mut().[2] = current.borrow().next.clone();
current.borrow_mut().next = temp;
node = current.borrow().[1].clone();
}
if let Some(last) = node [3] {
head = Some(last);
}
head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unwrap()' which can panic.
Mixing borrow and clone incorrectly.
✗ Incorrect
We use 'prev' pointer for swapping and moving, and clone the node to update head safely.
