Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the first node from the linked list.
DSA Rust
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
if head.is_none() {
return None;
}
let node = head.take().unwrap();
*head = node.[1];
Some(node.value)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes a compile error.
Assigning 'value' instead of 'next' breaks the list structure.
✗ Incorrect
The 'next' field points to the next node, so assigning head to node.next removes the first node.
2fill in blank
mediumComplete the code to safely pop the first node and return its value.
DSA Rust
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
head.take().map(|node| {
*head = node.[1];
node.value
})
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors.
Returning node.next instead of node.value returns wrong data.
✗ Incorrect
We assign head to node.next to remove the first node and keep the rest of the list.
3fill in blank
hardFix the error in the pop function to correctly update the head pointer.
DSA Rust
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
if let Some(node) = head.take() {
*head = node.[1];
Some(node.value)
} else {
None
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to node.value causes type mismatch.
Using node.prev causes compile errors.
✗ Incorrect
The head must be updated to node.next to remove the first node properly.
4fill in blank
hardFill both blanks to create a function that pops the first node and returns its value.
DSA Rust
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
if head.is_none() {
return None;
}
let node = head.[1]().unwrap();
*head = node.[2];
Some(node.value)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'clone' instead of 'take' causes ownership issues.
Assigning head to node.unwrap causes errors.
✗ Incorrect
Use 'take' to remove the node from head and 'next' to update head to the next node.
5fill in blank
hardFill all three blanks to implement pop that removes and returns the first node's value.
DSA Rust
fn pop(head: &mut Option<Box<Node>>) -> Option<i32> {
head.[1]().map(|node| {
*head = node.[2];
node.[3]
})
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'clone' instead of 'take' causes ownership errors.
Returning node.next instead of node.value returns wrong data.
✗ Incorrect
Use 'take' to remove the node, update head to node.next, and return node.value.
