Complete the code to initialize the previous pointer to nullptr.
ListNode* prev = [1];We start with prev as nullptr because initially there is no previous node.
Complete the code to store the next node before changing links.
ListNode* next = [1]->next;We save the next node from the current head before changing the link.
Fix the error in reversing the link by assigning the previous node to current node's next.
head->[1] = prev;To reverse the link, the current node's next should point to the previous node.
Fill both blanks to update pointers for the next iteration.
prev = [1]; head = [2];
After reversing the link, move prev to current head, and head to the saved next node.
Fill all three blanks to complete the iterative reverse function.
ListNode* reverseList(ListNode* head) {
ListNode* prev = [1];
while (head != nullptr) {
ListNode* next = head->next;
head->next = [2];
prev = head;
head = [3];
}
return prev;
}The function initializes prev to nullptr. Inside the loop, it reverses the link by setting head->next = prev. Then it moves prev to head and head to next. Finally, it returns prev as the new head.
