Complete the code to initialize the previous pointer to null.
ListNode prev = [1];We start reversing by setting the previous pointer to null because the new tail will point to null.
Complete the code to move the current pointer to the next node before changing links.
ListNode next = [1].next;We save the next node of current before changing current's next pointer.
Fix the error in reversing the link by assigning current.next to the correct node.
current.next = [1];To reverse the link, current.next should point to prev, not next or itself.
Fill both blanks to update prev and current pointers correctly after reversing one link.
prev = [1]; current = [2];
After reversing, prev moves to current, and current moves to next to continue the loop.
Fill all three blanks to complete the iterative reverse function returning the new head.
public ListNode reverseList(ListNode head) {
ListNode prev = [1];
ListNode current = head;
while (current != null) {
ListNode next = current.[2];
current.next = [3];
prev = current;
current = next;
}
return prev;
}The function starts with prev as null, saves current.next in next, reverses current.next to prev, then moves pointers forward. Finally, prev is the new head.
