Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the base case for the recursive reverse function.
DSA Java
if (head == [1] || head.next == null) { return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of null for the base case.
Checking head.next.next instead of head.next.
✗ Incorrect
The base case returns the head when it is null or the next node is null, meaning the list is empty or has one node.
2fill in blank
mediumComplete the code to recursively reverse the rest of the list after the head.
DSA Java
ListNode newHead = [1](head.next);Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one defined.
Calling reverseList with head instead of head.next.
✗ Incorrect
The recursive call is made to reverseList to reverse the list starting from head.next.
3fill in blank
hardFix the error in linking the next node's next pointer to the current head.
DSA Java
head.next.[1] = head;Attempts:
3 left
💡 Hint
Common Mistakes
Using prev or pointer which are not defined.
Using head.next.prev which does not exist.
✗ Incorrect
We set head.next.next to head to reverse the link between nodes.
4fill in blank
hardFill both blanks to complete the code that breaks the old link and returns the new head.
DSA Java
head.next = [1]; return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning head instead of newHead.
Not setting head.next to null causing cycles.
✗ Incorrect
We set head.next to null to break the old link and return newHead as the new head of the reversed list.
5fill in blank
hardFill all three blanks to complete the recursive reverse function.
DSA Java
public ListNode reverseList(ListNode head) {
if (head == [1] || head.next == null) {
return head;
}
ListNode [2] = reverseList(head.next);
head.next.[3] = head;
head.next = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or pointers.
Forgetting to set head.next to null.
✗ Incorrect
The base case checks for null, newHead stores the reversed list, and head.next.next points back to head to reverse the link.
