Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle node of the linked list.
DSA Java
ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.[1]; fast = fast.next.next; }
Attempts:
3 left
š” Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because singly linked lists don't have 'prev'.
Using 'head' or 'child' are invalid references.
ā Incorrect
To find the middle node, the slow pointer moves one step at a time using 'next'.
2fill in blank
mediumComplete the code to split the linked list into two halves.
DSA Java
ListNode mid = getMiddle(head);
ListNode rightHalf = mid.[1];
mid.next = null;Attempts:
3 left
š” Hint
Common Mistakes
Using 'prev' causes errors because singly linked lists don't have 'prev'.
Using 'head' or 'child' are invalid references.
ā Incorrect
The right half starts from the node after mid, so we use 'next'.
3fill in blank
hardFix the error in the merge function to correctly merge two sorted lists.
DSA Java
if (l1.val < l2.val) { result = l1; result.next = merge(l1.next, [1]); } else { result = l2; result.next = merge([2], l2.next); }
Attempts:
3 left
š” Hint
Common Mistakes
Swapping l1 and l2 in recursive calls causes incorrect merges.
Using 'null' or 'head' breaks the recursion.
ā Incorrect
When l1.val < l2.val, we merge the rest starting from l1.next and l2.
4fill in blank
hardFill both blanks to complete the base case check in merge sort.
DSA Java
if (head == null || head.[1] == [2]) { return head; }
Attempts:
3 left
š” Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors.
Comparing to 'head' instead of 'null' is incorrect.
ā Incorrect
The base case returns when the list is empty or has one node (next is null).
5fill in blank
hardFill all three blanks to complete the merge sort function.
DSA Java
ListNode mid = [1](head); ListNode right = mid.[2]; mid.next = null; ListNode leftSorted = mergeSort(head); ListNode rightSorted = mergeSort(right); return [3](leftSorted, rightSorted);
Attempts:
3 left
š” Hint
Common Mistakes
Using wrong function names causes errors.
Not splitting the list properly breaks recursion.
ā Incorrect
We get the middle node, split at next, then merge sorted halves.
