Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node and insert it at the beginning of the linked list.
DSA Rust
let new_node = Box::new(Node { data: value, next: [1] });Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to None breaks the list.
Setting next to new_node causes a cycle.
Using Some(value) is incorrect because next expects an Option>.
✗ Incorrect
The new node's next pointer should point to the current head of the list to insert it at the beginning.
2fill in blank
mediumComplete the code to update the head pointer after inserting the new node.
DSA Rust
head = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to new_node without Some causes a type error.
Assigning head to None removes the list.
Assigning head to itself does not update the list.
✗ Incorrect
After insertion, head should point to the new node wrapped in Some.
3fill in blank
hardFix the error in the function signature to accept a mutable reference to the head pointer.
DSA Rust
fn insert_at_beginning(head: [1], value: i32) {Attempts:
3 left
💡 Hint
Common Mistakes
Using &Option> does not allow modification.
Using Option> passes ownership, not reference.
Using Box is incorrect for the head pointer.
✗ Incorrect
The function needs a mutable reference to modify the head pointer.
4fill in blank
hardFill both blanks to complete the insertion function that updates the head pointer correctly.
DSA Rust
fn insert_at_beginning(head: [1], value: i32) { let new_node = Box::new(Node { data: value, next: [2] }); *head = Some(new_node); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using head.clone() for next causes unnecessary cloning.
Using None for next breaks the list.
Not dereferencing head causes type mismatch.
✗ Incorrect
The function takes a mutable reference to head, and the new node's next points to the current head value dereferenced.
5fill in blank
hardFill all three blanks to implement a safe insert at beginning function with proper ownership and updating.
DSA Rust
fn insert_at_beginning(head: [1], value: i32) { let new_node = Box::new(Node { data: value, next: [2] }); *head = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using head.clone() causes unnecessary cloning.
Assigning *head = head.clone() causes infinite loop.
Not wrapping new_node in Some causes type error.
✗ Incorrect
The function takes a mutable reference to head, sets new_node.next to the current list by dereferencing head, and updates head to Some(new_node).
