3. What will be the output of the following code snippet after reversing the doubly linked list?
struct Node {
int data;
Node* next;
Node* prev;
};
// List: 1 <-> 2 <-> 3
Node* head = new Node{1, nullptr, nullptr};
Node* second = new Node{2, nullptr, head};
head->next = second;
Node* third = new Node{3, nullptr, second};
second->next = third;
// Reverse function
Node* reverse(Node* head) {
Node* curr = head;
Node* temp = nullptr;
while (curr != nullptr) {
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
curr = curr->prev;
}
if (temp != nullptr) head = temp->prev;
return head;
}
head = reverse(head);
// Print list
Node* tempPrint = head;
while (tempPrint != nullptr) {
std::cout << tempPrint->data << " ";
tempPrint = tempPrint->next;
}