Complete the code to initialize the previous pointer to NULL before starting the reversal.
struct Node* reverseList(struct Node* head) {
struct Node* prev = [1];
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}We start with prev as NULL because the new tail of the reversed list should point to NULL.
Complete the code to update the next pointer to the next node in the list inside the loop.
while (current != NULL) { [1] = current->next; current->next = prev; prev = current; current = next; }
The next pointer stores the next node before changing links, so we don't lose access to the rest of the list.
Fix the error in the return statement to return the new head of the reversed list.
return [1];
After the loop, prev points to the new head of the reversed list, so we return prev.
Fill both blanks to complete the while loop condition and update current pointer correctly.
while ([1] != NULL) { next = current->next; current->next = prev; prev = current; current = [2]; }
The loop continues while current is not NULL, and current moves forward by assigning it to next.
Fill all three blanks to complete the iterative reversal function correctly.
struct Node* reverseList(struct Node* head) {
struct Node* prev = [1];
struct Node* current = head;
struct Node* next = NULL;
while (current != [2]) {
next = current->next;
current->next = [3];
prev = current;
current = next;
}
return prev;
}Initialize prev to NULL, loop while current is not NULL, and set current->next to prev to reverse the link.
