Complete the code to define the base case for the recursive reverse function.
if (head == [1] || head->next == nullptr) { return head; }
The base case for recursion is when the list is empty (head == nullptr) or has only one node (head->next == nullptr). We return head in these cases.
Complete the code to recursively reverse the rest of the list after the head.
ListNode* newHead = [1](head->next);The function calls itself recursively on the next node to reverse the rest of the list.
Fix the error in the pointer reversal step to correctly reverse the link.
head->next->[1] = head;We set the next node's next pointer to point back to the current head to reverse the link.
Fill both blanks to complete the recursive reversal by breaking the old link and returning the new head.
head->next = [1]; return [2];
We set head->next to nullptr to break the old link and return newHead which is the head of the reversed list.
Fill all three blanks to complete the full recursive reverse function.
ListNode* reverseList(ListNode* head) {
if (head == [1] || head->next == nullptr) {
return head;
}
ListNode* newHead = reverseList(head->next);
head->next->[2] = head;
head->next = [3];
return newHead;
}The base case checks for nullptr. Then we reverse the link by setting head->next->next = head, break old link by head->next = nullptr, and return newHead.
