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: value, next: [1] });Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to Some(value) instead of None
Using head instead of None for next
✗ Incorrect
When creating a new node to push, its next pointer should be None initially.
2fill in blank
mediumComplete the code to link the new node to the current head of the list.
DSA Rust
new_node.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning None instead of head.take()
Assigning new_node to next causing self-reference
✗ Incorrect
The new node's next should point to the current head, which is taken using head.take().
3fill in blank
hardFix the error in updating the head pointer to the new node.
DSA Rust
head = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning new_node directly without Some()
Assigning None or head itself
✗ Incorrect
The head must be updated to point to the new node wrapped in Some().
4fill in blank
hardFill both blanks to complete the push function that adds a new node at the front.
DSA Rust
fn push(&mut self, value: i32) {
let mut new_node = Box::new(Node { data: value, next: [1] });
new_node.next = [2];
self.head = Some(new_node);
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to Some(value) initially
Assigning self.head directly without take()
✗ Incorrect
The new node is created with next as None, then its next is set to the current head taken by self.head.take().
5fill in blank
hardFill all three blanks to complete the push method that safely adds a new node at the front of the linked list.
DSA Rust
fn push(&mut self, value: i32) {
let mut new_node = Box::new(Node { data: value, next: [1] });
new_node.next = [2];
self.head = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Not using take() causing ownership errors
Assigning head directly without Some()
Setting next incorrectly
✗ Incorrect
The new node is created with next as None, then linked to the old head using self.head.take(), and finally head is updated to Some(new_node).
