Complete the code to initialize the previous node pointer to null.
ListNode prev = [1];We start with prev as null because the reversed list's tail will point to null.
Complete the code to store the next node before changing links.
ListNode next = [1].next;We save current.next in next to not lose the rest of the list when reversing links.
Fix the error in reversing the link by assigning the previous node correctly.
[1].next = prev;The current node's next pointer should point to the previous node to reverse the link.
Fill both blanks to move the pointers forward in the loop.
prev = [1]; current = [2];
After reversing the link, move prev to current and current to next to continue.
Fill all three blanks to complete the iterative reverse function.
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 = null. Inside the loop, it saves current.next in next, then reverses the link by setting current.next = prev. Finally, it moves prev and current forward. Returning prev gives the new head of the reversed list.
