03. Reverse a Doubly Linked List
β GFG solution to the Reverse a Doubly Linked List problem: efficiently reverse a doubly linked list by swapping next and prev pointers. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given the head of a doubly linked list. Your task is to reverse the doubly linked list and return its head.
A doubly linked list is a data structure where each node contains data and two pointers: one pointing to the next node and another pointing to the previous node. To reverse it, we need to swap these pointer directions for every node.
π Examples
Example 1
Input: 3 <-> 4 <-> 5
Output: 5 <-> 4 <-> 3
Explanation: After reversing the given doubly linked list the new list will be 5 <-> 4 <-> 3.
Example 2
Input: 75 <-> 122 <-> 59 <-> 196
Output: 196 <-> 59 <-> 122 <-> 75
Explanation: After reversing the given doubly linked list the new list will be 196 <-> 59 <-> 122 <-> 75.
π Constraints
$1 \le \text{number of nodes} \le 10^6$
$0 \le \text{node->data} \le 10^4$
β
My Approach
The optimal approach involves iterating through the doubly linked list and swapping the next and prev pointers for each node:
Iterative Pointer Swapping
Handle Edge Cases:
If the list is empty or has only one node, return the head as is.
Traverse and Swap:
Start from the head node.
For each node, swap its
next
andprev
pointers.Store the original
next
pointer in a temporary variable before swapping.Move to the next node using the stored reference.
Find New Head:
Continue until we reach the end of the original list.
The last node we process becomes the new head of the reversed list.
Return Result:
Return the new head of the reversed doubly linked list.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the doubly linked list. We need to visit each node exactly once to swap its pointers.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for temporary variables regardless of the input size.
π§βπ» Code (C++)
class Solution {
public:
Node *reverse(Node *head) {
if (!head || !head->next) return head;
Node *curr = head, *temp;
while (curr) {
temp = curr->next;
curr->next = curr->prev;
curr->prev = temp;
if (!temp) return curr;
curr = temp;
}
return head;
}
};
β Code (Java)
class Solution {
public Node reverse(Node head) {
if (head == null || head.next == null) return head;
Node curr = head, temp;
while (curr != null) {
temp = curr.next;
curr.next = curr.prev;
curr.prev = temp;
if (temp == null) return curr;
curr = temp;
}
return head;
}
}
π Code (Python)
class Solution:
def reverse(self, head):
if not head or not head.next:
return head
curr = head
while curr:
curr.next, curr.prev = curr.prev, curr.next
if not curr.prev:
return curr
curr = curr.prev
return head
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated