Reverse a Doubly Linked List | Practice | GeeksforGeeks

Given a doubly linked list of n elements. The task is to reverse the doubly linked list.
Example 1:
Input:
LinkedList: 3 <--> 4 <--> 5
Output: 5 4 3

Example 2:
Input:
LinkedList: 75 <--> 122 <--> 59 <--> 196
Output: 196 59 122 75

Your Task:
Your task is to complete the given function reverseDLL(), which takes head reference as argument and should reverse the elements so that the tail becomes the new head and all pointers are correctly pointed. You need to return the new head of the reversed list. The printing and verification is done by the driver code.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= number of nodes <= 103
0 <= value of nodes <= 103
0
someshwarjha710 hours ago
public static Node reverseDLL(Node  head)
{
    //Your code here
    Node pre = null;
    Node cur = head;
    Node tem;
    while(cur!=null){
        tem = cur.next;
        cur.prev = cur.next;
        cur.next = pre;
         pre = cur;
        cur = tem;
    }
    return pre;
}
0
rajeshsurana4 days ago
Simple Java Solution →
public static Node reverseDLL(Node  head)
{
    if (head ==null || head.next==null) return head;
    Node prev = null;
    Node curr = head;
    while (curr != null) {
        Node next = curr.next;
        curr.next = prev;
        prev = curr;
        prev.prev = next;
        curr = next;
    }
    return prev;
}
-1
hharshit81182 weeks ago
Node* reverseDLL(Node *a){   if(a == NULL || a->next == NULL){       return a;   }   Node* t = NULL;   Node* curr = a;   while(curr){       t = curr->prev;       curr->prev = curr->next;       curr->next = t;       curr = curr->prev;   }   return t->prev;}
0
joker383 weeks ago
c++ code
Node* curr=head;   Node* pre=NULL;   while(curr!=NULL)   {       Node* temp=curr->next;       curr->next=pre;       curr->prev=temp;       pre=curr;       curr=temp;   }   return pre;
0
aryansinha18184 weeks ago
C++ Optimized Code 
 
   Node *first = nullptr;   Node *second = head;   Node *third;   while(second){       third = second->next;       second->next = first;       second->prev = third;       first = second;       second = third;   }   return first;
0
siddharth1923co10684 weeks ago
struct Node* reverseDLL(struct Node * head){   struct Node*prev=NULL;   struct Node*curr=head;   struct Node*next;   while(curr!=NULL){       next=curr->next;       curr->next=prev;       prev=curr;       curr=next;   }   return prev;}
+1
namankumar25921 month ago
Recursive solution 
If you face difficulty in understanding this solution then just DRY run it.
You will easily get the catch of this approach. 
 
Node* reverseDLL(Node *head){   if(head == NULL){       return head;   }   if(head->next == NULL){       swap(head->next,head->prev);       return head;   }   Node *smallhead = reverseDLL(head->next);   swap(head->prev,head->next);   return smallhead;}
-1
mdozairq1 month ago
public static Node reverseDLL(Node  head)
{
    //Your code here
    Node curr = head;
    Node temp = null;
    if(head == null || head.next == null)
        return head;
        
    while(curr != null){
        temp = curr.prev;
        curr.prev = curr.next;
        curr.next = temp;
        
        curr=curr.prev;
    }
    
    return temp.prev;
    
    
}
0
maneshram20Premium1 month ago
BEST SOLUTION || EASY UNDERSTANDING || O(N) TIME || SPACE : O(1)
Node* reverseDLL(Node * head){
    Node* p = head,* q = head;
    int cnt = 0;
    while(q->next!=nullptr){
        q = q->next;
        cnt++;
    }
    cnt++;
    cnt = cnt>>1;
    while(cnt--){
        swap(p->data,q->data);
        p = p->next;
        q = q->prev;
    }
    return head;
}
0
as0042301 month ago
In C;
struct Node* reverseDLL(struct Node * head){   struct Node *prev = NULL;   struct Node *cur = head;   struct Node *next;   while(cur)   {       next = cur->next;       cur->next = prev;       prev = cur;       cur = next;   }   return prev;} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.