Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new linked list node with the given value.
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
impl ListNode {
fn new(val: i32) -> Self {
ListNode { val: [1], next: None }
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using None or 0 instead of the input parameter 'val'.
Assigning 'next' field to 'val'.
✗ Incorrect
The constructor uses the input parameter 'val' to set the node's value.
2fill in blank
mediumComplete the code to find the middle node of the linked list using the fast and slow pointer technique.
DSA Rust
fn find_middle(head: &Option<Box<ListNode>>) -> Option<&Box<ListNode>> {
let mut slow = head.as_ref();
let mut fast = head.as_ref();
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
slow = [1];
fast = fast.unwrap().next.as_ref().unwrap().next.as_ref();
}
slow
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving the fast pointer one step instead of two.
Moving slow pointer incorrectly or mixing pointers.
✗ Incorrect
The slow pointer moves one step at a time to find the middle node.
3fill in blank
hardFix the error in the merge function to correctly merge two sorted linked lists.
DSA Rust
fn merge(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match (l1, l2) {
(Some(mut node1), Some(mut node2)) => {
if node1.val < node2.val {
node1.next = [1];
Some(node1)
} else {
node2.next = merge(Some(node1), node2.next);
Some(node2)
}
}
(Some(node1), None) => Some(node1),
(None, Some(node2)) => Some(node2),
(None, None) => None,
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping parameters incorrectly in recursive merge call.
Passing wrong next pointers.
✗ Incorrect
When node1's value is smaller, we merge node1.next with l2 (Some(node2)).
4fill in blank
hardFill both blanks to split the linked list into two halves and recursively sort them.
DSA Rust
fn merge_sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head;
}
let mut slow = &head;
let mut fast = &head;
let mut prev = None;
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
prev = slow.as_ref();
slow = &slow.as_ref().unwrap().next;
fast = &fast.as_ref().unwrap().next.as_ref().unwrap().next;
}
let mid = slow.clone();
if let Some(mut p) = prev.cloned() {
p.next = None;
}
let left = merge_sort(head);
let right = merge_sort([1]);
merge(left, [2])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for recursive calls.
Not splitting the list correctly.
✗ Incorrect
We recursively sort the left half (head) and right half (mid), then merge them.
5fill in blank
hardFill all three blanks to complete the merge sort implementation for linked list.
DSA Rust
fn merge_sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head;
}
let mut slow = &head;
let mut fast = &head;
let mut prev = None;
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
prev = slow.as_ref();
slow = &slow.as_ref().unwrap().next;
fast = &fast.as_ref().unwrap().next.as_ref().unwrap().next;
}
let mid = slow.clone();
if let Some(mut p) = prev.cloned() {
p.next = [1];
}
let left = merge_sort(head);
let right = merge_sort([2]);
merge([3], right)
}Attempts:
3 left
💡 Hint
Common Mistakes
Not breaking the list properly by setting p.next to None.
Mixing left and right variables in recursive calls.
✗ Incorrect
We break the list by setting p.next to None, then recursively sort left and right halves and merge.
