00:00

Reverse a Doubly Linked List

Last updated:

Reversing a doubly linked list turns it around so that the old last node becomes the head and the old head becomes the last node.

Each node already has links in both directions, so nothing needs to be moved: swapping the prev and next references of every node is enough. The node that ends up with a null prev after its swap was the old last node, and it is the new head.

The figure shows a three-node list before and after reversal:

Doubly linked list 100, 200, 300 before reversal and 300, 200, 100 after reversal, with HEAD on the first node

Example: Reverse a Doubly Linked List in Java

Class: Node

Node.java
public class Node {
  int data;   // Data stored in the node
  Node prev;  // Reference to the previous node in the list
  Node next;  // Reference to the next node in the list

  // Constructor to initialize a new node with data
  Node(int new_data) {
    data = new_data;
    prev = null;
    next = null;
  }
}

The program below swaps the links recursively: reverseList swaps one node's links and then calls itself on the next node of the original list, which after the swap is reached through prev. FAQ Q1 shows the same idea written with a loop.

Class: ReverseDoublyLinkedList

ReverseDoublyLinkedList.java
// Java program to reverse a doubly linked list using recursion approach
public class ReverseDoublyLinkedList {
  static Node head;

  // Method to insert a node in doubly linked list
  static void insertNode(int data) {
    Node newNode = new Node(data);
    Node last = head;
    if (head == null) {
      head = newNode;
      return;
    }
    while (last.next != null) {
      last = last.next;
    }
    last.next = newNode;
    newNode.prev = last;
  }

  // Method to reverse a doubly linked list using recursive approach
  static Node reverseList(Node node) {

    // If node is null that means list is empty
    if (node == null)
      return null;

    // Swap the next and previous pointers
    Node temp = node.prev;
    node.prev = node.next;
    node.next = temp;

    // If prev is null after swap, this is the new head
    if (node.prev == null) {
      return node;
    }

    // Recurse for the next node
    return reverseList(node.prev);
  }

  // Method to print a list
  static void printList(Node node) {
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Insert node into a doubly linked list
    insertNode(10);
    insertNode(20);
    insertNode(30);
    insertNode(40);
    insertNode(50);

    System.out.println("Original doubly linked list:");
    printList(head);

    // Reverse the doubly linked list
    head = reverseList(head);

    System.out.println("Reversed doubly linked list:");
    printList(head);
  }
}
Output
Original doubly linked list:
10 20 30 40 50
Reversed doubly linked list:
50 40 30 20 10

Time Complexity: O(n), where n is the number of nodes.

Extra Space: O(n) for this recursive version, because Java keeps one stack frame per call (the JVM does not eliminate tail calls). The loop in FAQ Q1 needs only O(1).