Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle node of the linked list using slow and fast pointers.
DSA C#
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 the pointer to move backwards or fail.
Using 'child' or 'random' which are not standard linked list pointers.
✗ Incorrect
The slow pointer moves one step at a time using the 'next' reference 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 C#
ListNode prev = null; ListNode curr = slow; while (curr != null) { ListNode nextTemp = curr.[1]; curr.[2] = prev; prev = curr; curr = nextTemp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes incorrect traversal.
Using non-existent pointers like 'child' or 'random'.
✗ Incorrect
To reverse the list, we store the next node using 'next', then reverse the pointer.
3fill in blank
hardFix the error in the merging loop to reorder the list by alternating nodes from the first and reversed second half.
DSA C#
ListNode first = head; ListNode second = prev; while (second.next != null) { ListNode temp1 = first.[1]; ListNode temp2 = second.[2]; first.[3] = second; second.[4] = temp1; first = temp1; second = temp2; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' causes incorrect pointer assignments.
Mixing different pointer names leads to runtime errors.
✗ Incorrect
All pointer moves and assignments use the 'next' pointer to correctly merge the two halves.
4fill in blank
hardFill both blanks to correctly split the list into two halves by breaking the link at the middle.
DSA C#
ListNode slow = head; ListNode fast = head; ListNode prev = null; while (fast != null && fast.next != null) { prev = slow; slow = slow.[1]; fast = fast.next.next; } prev.[2] = null;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for moving slow pointer.
Not breaking the list properly causes cycles.
✗ Incorrect
We move slow using 'next' and then break the list by setting prev.next to null.
5fill in blank
hardFill all three blanks to complete the reorder list function that combines all steps.
DSA C#
public void ReorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head;
ListNode fast = head;
ListNode prev = null;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.[1];
fast = fast.next.next;
}
prev.[2] = null;
ListNode second = null;
ListNode curr = slow;
ListNode prevNode = null;
while (curr != null) {
ListNode nextTemp = curr.[3];
curr.next = prevNode;
prevNode = curr;
curr = nextTemp;
}
second = prevNode;
ListNode first = head;
while (second != null) {
ListNode temp1 = first.next;
ListNode temp2 = second.next;
first.next = second;
second.next = temp1;
first = temp1;
second = temp2;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect pointers like 'prev' or 'child' causes errors.
Not breaking the list properly leads to infinite loops.
✗ Incorrect
All pointer moves and breaks use the 'next' pointer to correctly reorder the list.
