Complete the code to check the base case for recursion in reversing a singly linked list.
if (head == [1] || head.next == null) return head;
The base case for recursion is when the list is empty (head is null) or has only one node (head.next is null). Here, we check if head is null.
Complete the code to recursively reverse the rest of the list after the head.
ListNode newHead = [1](head.next);The recursive call should call the same method name, here assumed as ReverseList, passing head.next to reverse the rest of the list.
Fix the error in linking the next node back to the current head during reversal.
head.next.next = [1];To reverse the link, the next node's next pointer should point back to the current head.
Fill both blanks to complete the reversal step and break the old link.
head.next = [1]; return [2];
After reversing, set head.next to null to break old link, and return newHead as the new head of reversed list.
Fill all three blanks to complete the full recursive reverse method.
public ListNode ReverseList(ListNode head) {
if (head == [1] || head.next == null) return head;
ListNode newHead = ReverseList([2]);
head.next.next = [3];
head.next = null;
return newHead;
}The base case checks if head is null. The recursive call passes head.next. Then, head.next.next points back to head to reverse the link.
