Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle node of the linked list using the slow and fast pointer technique.
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 fast.next instead of slow.next for the slow pointer.
Using prev which is not defined in singly linked list.
✗ Incorrect
The slow pointer moves one step at a time using slow.next to find the middle node.
2fill in blank
mediumComplete the code to reverse the second half of the linked list starting from the middle node.
DSA Java
ListNode prev = null; ListNode curr = slow; while (curr != null) { ListNode nextTemp = curr.[1]; curr.next = prev; prev = curr; curr = nextTemp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next to get the next node.
Not storing the next node before reversing the link.
✗ Incorrect
We store the next node using curr.next before reversing the link.
3fill in blank
hardFix the error in the merging step to reorder the list by alternating nodes from the first and reversed second half.
DSA Java
ListNode first = head; ListNode second = prev; while (second.next != null) { ListNode tmp1 = first.next; ListNode tmp2 = second.next; first.next = second; second.next = tmp1; first = tmp1; second = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving second to first or tmp1 which breaks the merge.
Not updating second pointer causing infinite loop.
✗ Incorrect
After linking, move second pointer to tmp2 which is the next node in reversed half.
4fill in blank
hardFill both blanks to complete the method that reorders the linked list as per the problem statement.
DSA Java
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.[1];
fast = fast.next.next;
}
ListNode prev = null, curr = slow;
while (curr != null) {
ListNode nextTemp = curr.[2];
curr.next = prev;
prev = curr;
curr = nextTemp;
}
ListNode first = head, second = prev;
while (second.next != null) {
ListNode tmp1 = first.next;
ListNode tmp2 = second.next;
first.next = second;
second.next = tmp1;
first = tmp1;
second = tmp2;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'curr' instead of 'next' causing null pointer exceptions.
Mixing up pointer movements leading to incorrect list structure.
✗ Incorrect
Both blanks require 'next' to traverse the list correctly.
5fill in blank
hardFill all three blanks to create a dictionary comprehension-like map of node values to their positions during reordering (simulate with Java Map).
DSA Java
Map<Integer, Integer> nodePositions = new HashMap<>(); int pos = 0; ListNode current = head; while (current != null) { nodePositions.put(current.[1], [2]); current = current.[3]; pos++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'val' which is not defined.
Using 'prev' or other invalid pointers.
Not incrementing position correctly.
✗ Incorrect
Use 'val' for node value, 'pos' for position, and 'next' to move to next node.
