### Detailed Notes on the Approach and Solution
#### Problem Statement Given a doubly linked list (DLL), we need to reverse the list and return the head of the modified list.
#### Class Structure The `Node` class is structured as follows: ```cpp class Node { public: int data; Node *next, *prev; Node() { this->data = 0; next = NULL; prev = NULL; } Node(int data) { this->data = data; this->next = NULL; this->prev = NULL; } Node(int data, Node* next, Node *prev) { this->data = data; this->next = next; this->prev = prev; } }; ```
#### Approach 1. **Initial Check**: - If the `head` is `nullptr`, it means the list is empty. Return `nullptr` immediately as there is nothing to reverse.
2. **Traversal and Swapping**: - Initialize a `current` pointer to the `head` of the list. - Initialize a `temp` pointer to `nullptr` which will be used for swapping pointers. - Traverse through the list using a `while` loop that continues as long as `current` is not `nullptr`.
3. **Swapping Pointers**: - For each node, swap its `next` and `prev` pointers: - `temp` is set to `current->prev` (temporarily store the previous pointer). - `current->prev` is set to `current->next` (swap the `prev` pointer to point to the next node). - `current->next` is set to `temp` (swap the `next` pointer to point to the previous node). - Move the `current` pointer to the next node in the original list, which is `current->prev` after swapping.
4. **Updating the Head**: - After the loop, `temp` will be pointing to the previous node of the original head. This is because `temp` was the previous node stored before the last swap. - The new head of the reversed list will be `temp->prev`, as `temp` itself is pointing to the old head node.
5. **Return the New Head**: - Return the updated `head`.
#### Detailed Explanation of the Code ```cpp Node* reverseDLL(Node* head) { if (!head) return nullptr;
Node* current = head; Node* temp = nullptr;
// Traverse the list while (current != nullptr) { // Swap the next and prev pointers temp = current->prev; current->prev = current->next; current->next = temp; // Move to the next node (which is previous before swapping) current = current->prev; }
// After the loop, temp will be pointing to the new head // temp->prev will be nullptr because it was the next of the old head if (temp != nullptr) { head = temp->prev; }
return head; } ```
1. **Initial Check**: ```cpp if (!head) return nullptr; ``` - This line checks if the `head` is `nullptr`. If it is, the function returns `nullptr` because there is no list to reverse.
2. **Initialize Pointers**: ```cpp Node* current = head; Node* temp = nullptr; ``` - `current` is initialized to the `head` of the list to start traversal. - `temp` is initialized to `nullptr` to assist in swapping pointers.
3. **Traversal and Swapping**: ```cpp while (current != nullptr) { // Swap the next and prev pointers temp = current->prev; current->prev = current->next; current->next = temp; // Move to the next node (which is previous before swapping) current = current->prev; } ``` - The `while` loop traverses the list until `current` becomes `nullptr`. - Within the loop, the `next` and `prev` pointers of the `current` node are swapped. - `temp` temporarily stores the `prev` pointer of the `current` node. - The `prev` pointer of the `current` node is updated to its `next` pointer. - The `next` pointer of the `current` node is updated to the `temp` pointer. - `current` is moved to the `prev` node, which is the original next node due to swapping.
4. **Update Head**: ```cpp if (temp != nullptr) { head = temp->prev; } ``` - After the loop, `temp` points to the previous node of the original head. - `head` is updated to `temp->prev` to set the new head of the reversed list.
5. **Return the New Head**: ```cpp return head; ``` - The function returns the updated `head`, which is now the head of the reversed list.
#### Example Given a list `4 <-> 3 <-> 2 <-> 1`: - After reversing, the list becomes `1 <-> 2 <-> 3 <-> 4`.
### Summary This approach efficiently reverses a doubly linked list by swapping the `next` and `prev` pointers for each node during traversal and updating the head to the new head of the reversed list.