POTD Solutions | 18 Nov’ 23 | Reverse a Doubly Linked List Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Linked Lists but will also help you build up problem-solving skills. POTD Solution 18 November 2023 We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution. POTD 18th NovReverse a Doubly Linked List Solve! Watch! POTD 18 November: Reverse a Doubly Linked List:Given a doubly linked list of n elements. Your task is to reverse the doubly linked list in-place. Examples: Input: LinkedList: 3 <--> 4 <--> 5Output: 5 <--> 4 <--> 3 Input: LinkedList: 75 <--> 122 <--> 59 <--> 196Output: 196 <--> 59 <--> 122 <--> 75 Reverse a Doubly Linked List by Iterative Approach:The idea is to use an iterative approach. We can modify the prev and next pointers of each node to reverse the direction of the links. Step-by-step approach: Initialize two pointers, curr and prev. Initially, curr points to the head of the list, and prev is set to null. Enter a loop that iterates through the entire list. Inside the loop:Update the curr node's prev pointer to its next pointer, effectively reversing the direction of the link.Update the curr node's next pointer to prev, again reversing the direction of the link.Essentially, the prev and next pointers are swapped to reverse the node's connections in the DLL.Once the loop is complete, the curr pointer will be pointing to the new head of the reversed list, and the prev pointer will be pointing to the node that was originally at the end of the list (now the new tail). Return prev to indicate the new head of the reversed DLL.Below is the implementation of the above approach: C++ class Solution { public: // Function to reverse a doubly linked list Node* reverseDLL(Node * head) { // If the list is empty or has only one node, return the head if(head == NULL || head->next == NULL) return head; Node *prev = NULL, *curr = head; // Traverse through the list and swap the previous and next pointers while(curr != NULL){ prev = curr->prev; // Store the previous node curr->prev = curr->next; // Set the previous pointer to the next node curr->next = prev; // Set the next pointer to the previous node curr = curr->prev; // Move to the next node } return prev->prev; // Return the new head of the reversed list } }; Java //Function to reverse a doubly linked list public static Node reverseDLL(Node head) { //checking if head is null or head.next is null, //if true, then return the head itself as there is no need to reverse a list with 0 or 1 node if(head == null || head.next == null) return head; //declaring a current and previous node Node curr = head, prev = null; //looping through the list while(curr != null){ //storing the previous node in prev prev = curr.prev; //swapping the prev and next pointers of the current node curr.prev = curr.next; curr.next = prev; //moving the current node to its previous node curr = curr.prev; } //returning the previous node of head as the new head (the last node after reversing the list) return prev.prev; } Python3 class Solution: def reverseDLL(self, head): # Initialize temporary variables temp = None current = head # Reverse the doubly linked list while current is not None: # Swap previous and next pointers of the current node temp = current.prev current.prev = current.next current.next = temp current = current.prev # Update the head if necessary if temp is not None: head = temp.prev return head Time Complexity : O(N), where N is the number of nodes in the doubly linked list.Auxiliary Space: O(1), constant space POTD 18th NovReverse a Doubly Linked ListRead Full Solution! Comment More infoAdvertise with us Next Article Reverse a sublist of linked list M mridul_gfg Follow Improve Article Tags : DSA doubly linked list GFG-POTD-Solutions Similar Reads Reverse a Doubly Linked List without swapping nodes Write a program to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Approach: In the previous post, doubly linked list is being reversed by swapping prev and next pointers for all nodes, changing prev of the head (o 10 min read Reverse a Doubly linked list using recursion Given a doubly linked list. Reverse it using recursion. Original Doubly linked list Reversed Doubly linked list We have discussed Iterative solution to reverse a Doubly Linked List Algorithm: If list is empty, return Reverse head by swapping head->prev and head->next If prev = NULL it means th 9 min read Reverse a Doubly Linked List by swapping data Given a Doubly Linked List, we are asked to reverse the list in place without using any extra space. Examples: Input : 1 <--> 2 <--> 5 <--> 6 <--> 7 Output : 7 <--> 6 <--> 5 <--> 2 <--> 1 Input : 11 <--> 22 <--> 33 <--> 22 <--> 10 min read Reverse a Doubly Linked List Given a Doubly Linked List, the task is to reverse the Doubly Linked List.Examples:Input: Doubly Linked List = 1 <-> 2 <-> 3 -> NULL Doubly Linked List Output: Reversed Doubly Linked List = 3 <-> 2 <-> 1 -> NULLReversed Doubly Linked ListInput: Doubly Linked List = 1 - 15 min read Reverse a sublist of linked list Given a linked list and positions m and n. We need to reverse the linked list from position m to n.Examples: Input : linkedlist : 10->20->30->40->50->60->70->NULL , m = 3 and n = 6Output : 10->20->60->50->40->30->70->NULLExplanation: Linkedlist reversed star 15+ min read Print Doubly Linked list in Reverse Order Given a doubly-linked list of positive integers. The task is to print the given doubly linked list data in reverse order. Examples: Input: List = 1 <=> 2 <=> 3 <=> 4 <=> 5 Output: 5 4 3 2 1 Input: 10 <=> 20 <=> 30 <=> 40 Output: 40 30 20 10 Approach: Take a 7 min read Like