Complete the code to swap the next and prev pointers of the current node.
void reverse(Node* &head) {
Node* current = head;
while (current != nullptr) {
Node* temp = current->next;
current->next = [1];
current->prev = temp;
current = temp;
}
}We swap the next and prev pointers by assigning current->next to current->prev.
Complete the code to update the head pointer after reversing the list.
void reverse(Node* &head) {
Node* current = head;
Node* temp = nullptr;
while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr) {
head = [1];
}
}After the loop, temp points to the old previous node, so head should be updated to temp->prev to point to the new head.
Fix the error in the loop condition to correctly traverse the list during reversal.
void reverse(Node* &head) {
Node* current = head;
Node* temp = nullptr;
while ([1]) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr) {
head = temp->prev;
}
}The loop should continue while current is not nullptr to traverse all nodes.
Fill both blanks to complete the function that reverses a doubly linked list and updates the head.
void reverse(Node* &head) {
Node* current = head;
Node* temp = nullptr;
while (current != nullptr) {
temp = current->[1];
current->[2] = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr) {
head = temp->prev;
}
}We assign temp to current->prev and then set current->prev to current->next to swap pointers correctly.
Fill all three blanks to create a function that reverses a doubly linked list and updates the head pointer.
void reverse(Node* &head) {
Node* current = head;
Node* temp = nullptr;
while (current != nullptr) {
temp = current->[1];
current->[2] = current->next;
current->next = temp;
current = current->[3];
}
if (temp != nullptr) {
head = temp->prev;
}
}All three blanks should be 'prev' to correctly swap pointers and move current backward.
