class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function reverseList(head: ListNode | null): ListNode | null { if (!head || !head.next) return head; const newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } // Create list: 1 -> 2 -> 3 -> null const node3 = new ListNode(3); const node2 = new ListNode(2, node3); const node1 = new ListNode(1, node2); const reversedHead = reverseList(node1); // Function to print list function printList(head: ListNode | null): string { let current = head; let result = ''; while (current) { result += current.val + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(reversedHead));
The recursive function reverses the list by moving to the end and then changing the next pointers backward. The original list 1 -> 2 -> 3 becomes 3 -> 2 -> 1 after reversal.
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function reverseList(head: ListNode | null): ListNode | null { if (!head || !head.next) return head; const newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } const node = new ListNode(5); const reversedHead = reverseList(node); function printList(head: ListNode | null): string { let current = head; let result = ''; while (current) { result += current.val + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(reversedHead));
Since the list has only one node, the recursive function returns it immediately without changes. The output remains '5 -> null'.
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function reverseList(head: ListNode | null): ListNode | null { if (!head || !head.next) return head; const newHead = reverseList(head.next); head.next = head; head.next = null; return newHead; } const node3 = new ListNode(3); const node2 = new ListNode(2, node3); const node1 = new ListNode(1, node2); const reversedHead = reverseList(node1); function printList(head: ListNode | null): string { let current = head; let result = ''; while (current) { result += current.val + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(reversedHead));
The line 'head.next = head;' incorrectly tries to assign the node to its own next pointer before setting it to null, causing a TypeError when trying to access 'head.next.next'.
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function reverseList(head: ListNode | null): ListNode | null { if (!head || !head.next) return head; const newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } const node4 = new ListNode(1); const node3 = new ListNode(2, node4); const node2 = new ListNode(2, node3); const node1 = new ListNode(4, node2); const reversedHead = reverseList(node1); function printList(head: ListNode | null): string { let current = head; let result = ''; while (current) { result += current.val + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(reversedHead));
The recursive reversal flips the list so the last node (1) becomes the head, followed by the two 2's, then 4 at the end.
'head.next.next = head' points the next node's next pointer back to the current node, effectively reversing the direction of the link. Assigning 'head.next = head' would make the current node point to itself, creating a loop and breaking the list structure.
