The Wayback Machine - https://web.archive.org/web/20240717181823/https://www.geeksforgeeks.org/reverse-doubly-linked-list-using-recursion/
Open In App

Reverse a Doubly linked list using recursion

Last Updated : 17 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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:

  1. If list is empty, return 
  2. Reverse head by swapping head->prev and head->next 
  3. If prev = NULL it means that list is fully reversed. Else reverse(head->prev) 

Implementation:

C++




// C++ implementation to reverse a doubly
// linked list using recursion
#include <bits/stdc++.h>
using namespace std;
 
// a node of the doubly linked list
struct Node {
    int data;
    Node *next, *prev;
};
 
// function to get a new node
Node* getNode(int data)
{
    // allocate space
    Node* new_node = new Node;
    new_node->data = data;
    new_node->next = new_node->prev = NULL;
    return new_node;
}
 
// function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, Node* new_node)
{
    // since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
 
    // link the old list of the new node
    new_node->next = (*head_ref);
 
    // change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    // move the head to point to the new node
    (*head_ref) = new_node;
}
 
// function to reverse a doubly linked list
Node* Reverse(Node* node)
{
    // If empty list, return
    if (!node)
        return NULL;
 
    // Otherwise, swap the next and prev
    Node* temp = node->next;
    node->next = node->prev;
    node->prev = temp;
 
    // If the prev is now NULL, the list
    // has been fully reversed
    if (!node->prev)
        return node;
 
    // Otherwise, keep going
    return Reverse(node->prev);
}
 
// Function to print nodes in a given doubly
// linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
 
// Driver program to test above
int main()
{
    // Start with the empty list
    Node* head = NULL;
 
    // Create doubly linked: 10<->8<->4<->2 */
    push(&head;, getNode(2));
    push(&head;, getNode(4));
    push(&head;, getNode(8));
    push(&head;, getNode(10));
    cout << "Original list: ";
    printList(head);
 
    // Reverse doubly linked list
    head = Reverse(head);
    cout << "\nReversed list: ";
    printList(head);
    return 0;
}


Java




// Java implementation to reverse a doubly
// linked list using recursion
class GFG
{
     
// a node of the doubly linked list
static class Node
{
    int data;
    Node next, prev;
};
 
// function to get a new node
static Node getNode(int data)
{
    // allocate space
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = new_node.prev = null;
    return new_node;
}
 
// function to insert a node at the beginning
// of the Doubly Linked List
static Node push(Node head_ref, Node new_node)
{
    // since we are adding at the beginning,
    // prev is always null
    new_node.prev = null;
 
    // link the old list of the new node
    new_node.next = (head_ref);
 
    // change prev of head node to new node
    if ((head_ref) != null)
        (head_ref).prev = new_node;
 
    // move the head to point to the new node
    (head_ref) = new_node;
    return head_ref;
}
 
// function to reverse a doubly linked list
static Node Reverse(Node node)
{
    // If empty list, return
    if (node == null)
        return null;
 
    // Otherwise, swap the next and prev
    Node temp = node.next;
    node.next = node.prev;
    node.prev = temp;
 
    // If the prev is now null, the list
    // has been fully reversed
    if (node.prev == null)
        return node;
 
    // Otherwise, keep going
    return Reverse(node.prev);
}
 
// Function to print nodes in a given doubly
// linked list
static void printList(Node head)
{
    while (head != null)
    {
        System.out.print( head.data + " ");
        head = head.next;
    }
}
 
// Driver code
public static void main(String args[])
{
    // Start with the empty list
    Node head = null;
 
    // Create doubly linked: 10<.8<.4<.2 /
    head = push(head, getNode(2));
    head = push(head, getNode(4));
    head = push(head, getNode(8));
    head = push(head, getNode(10));
    System.out.print( "Original list: ");
    printList(head);
 
    // Reverse doubly linked list
    head = Reverse(head);
    System.out.print("\nReversed list: ");
    printList(head);
}
}
 
// This code is contributed by Arnab Kundu


Python3




# Python3 implementation to reverse a doubly
# linked list using recursion
import math
 
# a node of the doubly linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# function to get a new node
def getNode(data):
     
    # allocate space
    new_node = Node(data)
    new_node.data = data
    new_node.next = new_node.prev = None
    return new_node
 
# function to insert a node at the beginning
# of the Doubly Linked List
def push(head_ref, new_node):
     
    # since we are adding at the beginning,
    # prev is always None
    new_node.prev = None
 
    # link the old list of the new node
    new_node.next = head_ref
 
    # change prev of head node to new node
    if (head_ref != None):
        head_ref.prev = new_node
 
    # move the head to point to the new node
    head_ref = new_node
    return head_ref
 
# function to reverse a doubly linked list
def Reverse(node):
     
    # If empty list, return
    if not node:
        return None
 
    # Otherwise, swap the next and prev
    temp = node.next
    node.next = node.prev
    node.prev = temp
 
    # If the prev is now None, the list
    # has been fully reversed
    if not node.prev:
        return node
 
    # Otherwise, keep going
    return Reverse(node.prev)
 
# Function to print nodes in a given doubly
# linked list
def printList(head):
    while (head != None) :
        print(head.data, end = " ")
        head = head.next
     
# Driver Code
if __name__=='__main__':
     
    # Start with the empty list
    head = None
 
    # Create doubly linked: 10<.8<.4<.2 */
    head = push(head, getNode(2));
    head = push(head, getNode(4));
    head = push(head, getNode(8));
    head = push(head, getNode(10));
    print("Original list: ", end = "")
    printList(head)
     
    # Reverse doubly linked list
    head = Reverse(head)
    print("\nReversed list: ", end = "")
    printList(head)
     
# This code is contributed by Srathore


C#




// C# implementation to reverse a doubly
using System;
 
// linked list using recursion
class GFG
{
     
// a node of the doubly linked list
public class Node
{
    public int data;
    public Node next, prev;
};
 
// function to get a new node
static Node getNode(int data)
{
    // allocate space
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = new_node.prev = null;
    return new_node;
}
 
// function to insert a node at the beginning
// of the Doubly Linked List
static Node push(Node head_ref, Node new_node)
{
    // since we are adding at the beginning,
    // prev is always null
    new_node.prev = null;
 
    // link the old list of the new node
    new_node.next = (head_ref);
 
    // change prev of head node to new node
    if ((head_ref) != null)
        (head_ref).prev = new_node;
 
    // move the head to point to the new node
    (head_ref) = new_node;
    return head_ref;
}
 
// function to reverse a doubly linked list
static Node Reverse(Node node)
{
    // If empty list, return
    if (node == null)
        return null;
 
    // Otherwise, swap the next and prev
    Node temp = node.next;
    node.next = node.prev;
    node.prev = temp;
 
    // If the prev is now null, the list
    // has been fully reversed
    if (node.prev == null)
        return node;
 
    // Otherwise, keep going
    return Reverse(node.prev);
}
 
// Function to print nodes in a given doubly
// linked list
static void printList(Node head)
{
    while (head != null)
    {
        Console.Write( head.data + " ");
        head = head.next;
    }
}
 
// Driver code
public static void Main(String []argsS)
{
    // Start with the empty list
    Node head = null;
 
    // Create doubly linked: 10<.8<.4<.2 /
    head = push(head, getNode(2));
    head = push(head, getNode(4));
    head = push(head, getNode(8));
    head = push(head, getNode(10));
    Console.Write( "Original list: ");
    printList(head);
 
    // Reverse doubly linked list
    head = Reverse(head);
    Console.Write("\nReversed list: ");
    printList(head);
}
}
 
// This code is contributed by Arnab Kundu


Javascript




<script>
// javascript implementation to reverse a doubly
// linked list using recursion    
// a node of the doubly linked list
class Node {
    constructor(val) {
        this.data = val;
        this.prev = null;
        this.next = null;
    }
}
 
    // function to get a new node
    function getNode(data) {
        // allocate space
var new_node = new Node();
        new_node.data = data;
        new_node.next = new_node.prev = null;
        return new_node;
    }
 
    // function to insert a node at the beginning
    // of the Doubly Linked List
    function push(head_ref,  new_node) {
        // since we are adding at the beginning,
        // prev is always null
        new_node.prev = null;
 
        // link the old list of the new node
        new_node.next = (head_ref);
 
        // change prev of head node to new node
        if ((head_ref) != null)
            (head_ref).prev = new_node;
 
        // move the head to point to the new node
        (head_ref) = new_node;
        return head_ref;
    }
 
    // function to reverse a doubly linked list
    function Reverse(node) {
        // If empty list, return
        if (node == null)
            return null;
 
        // Otherwise, swap the next and prev
var temp = node.next;
        node.next = node.prev;
        node.prev = temp;
 
        // If the prev is now null, the list
        // has been fully reversed
        if (node.prev == null)
            return node;
 
        // Otherwise, keep going
        return Reverse(node.prev);
    }
 
    // Function to print nodes in a given doubly
    // linked list
    function printList(head) {
        while (head != null) {
            document.write(head.data + " ");
            head = head.next;
        }
    }
 
    // Driver code
     
        // Start with the empty list
var head = null;
 
        // Create doubly linked: 10<.8<.4<.2 /
        head = push(head, getNode(2));
        head = push(head, getNode(4));
        head = push(head, getNode(8));
        head = push(head, getNode(10));
        document.write("Original list: ");
        printList(head);
 
        // Reverse doubly linked list
        head = Reverse(head);
        document.write("<br/>Reversed list: ");
        printList(head);
 
// This code contributed by umadevi9616
</script>


Output

Original list: 10 8 4 2 
Reversed list: 2 4 8 10 

Time Complexity: O(N), where N is the number of nodes in given doubly linked list.
Auxiliary Space: O(N), due to recursion call stack.



Previous Article
Next Article

Similar Reads

Reverse a linked list using recursion
Given pointer to the head node of a linked list, the task is to recursively reverse the linked list. We need to reverse the list by changing links between nodes.  Examples:  Input: Head: 1-&gt;2-&gt;3-&gt;4-&gt;NULLOutput: Reversed Linked list : 4-&gt;3-&gt;2-&gt;1-&gt;NULL Input: Head: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULLOutput: Reversed Linked list
12 min read
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 (or start) and then changing the head pointer in the
10 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 &lt;--&gt; 2 &lt;--&gt; 5 &lt;--&gt; 6 &lt;--&gt; 7 Output : 7 &lt;--&gt; 6 &lt;--&gt; 5 &lt;--&gt; 2 &lt;--&gt; 1 Input : 11 &lt;--&gt; 22 &lt;--&gt; 33 &lt;--&gt; 22 &lt;--&gt; 1 Output : 1 &lt;--&gt; 22 &lt;--&gt; 33 &lt;--
10 min read
Reverse a doubly linked list in groups of given size
Given a doubly linked list containing n nodes. The problem is to reverse every group of k nodes in the list. Examples: Prerequisite: Reverse a doubly linked list | Set-2. Approach: Create a recursive function say reverse(head, k). This function receives the head or the first node of each group of k nodes. It reverses those groups of k nodes by appl
15+ min read
Reverse a doubly circular linked list
The problem is to reverse the given doubly circular linked list. Examples: Input: Output: Algorithm: insertEnd(head, new_node) Declare last if head == NULL then new_node-&gt;next = new_node-&gt;prev = new_node head = new_node return last = head-&gt;prev new_node-&gt;next = head head-&gt;prev = new_node new_node-&gt;prev = last last-&gt;next = new_n
11 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 &lt;=&gt; 2 &lt;=&gt; 3 &lt;=&gt; 4 &lt;=&gt; 5 Output: 5 4 3 2 1 Input: 10 &lt;=&gt; 20 &lt;=&gt; 30 &lt;=&gt; 40 Output: 40 30 20 10 Approach: Take a pointer to point to head of the doubly linked list
7 min read
Reverse a doubly linked list in groups of given size | Set 2
Given a doubly-linked list containing n nodes. The problem is to reverse every group of k nodes in the list. Examples: Input: List: 10&lt;-&gt;8&lt;-&gt;4&lt;-&gt;2, K=2Output: 8&lt;-&gt;10&lt;-&gt;2&lt;-&gt;4 Input: List: 1&lt;-&gt;2&lt;-&gt;3&lt;-&gt;4&lt;-&gt;5&lt;-&gt;6&lt;-&gt;7&lt;-&gt;8, K=3Output: 3&lt;-&gt;2&lt;-&gt;1&lt;-&gt;6&lt;-&gt;5
15+ min read
POTD Solutions | 18 Nov’ 23 | Reverse a Doubly Linked List
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. We recommend you to try this problem
4 min read
Reverse a Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. Example: Input: Output: Recommended PracticeReverse a Doubly Linked ListTry It! Follow the given steps to solve the problem using the above approach: Traverse the linked list using a pointerSwap the prev and next pointers for all nodesAt last, change the head pointer o
15+ min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
three90RightbarBannerImg