1. Introduction

A Doubly Linked List (DLL) is a list in which each node contains a data part and two pointers. The two pointers are next and prev, which point to the next and previous node in the sequence, respectively. This structure allows traversal in both directions: forward and backward. Reversing a DLL means changing the next and previous pointers of each node so that the direction of the list is reversed.

2. Program Steps

1. Initialize three pointers: current, prev, and next.

2. Traverse the list using the current pointer.

3. For each node:

a. Save its next node to the next pointer.

b. Update its next pointer to the prev node.

c. Update its prev pointer to the next node.

d. Move the prev pointer to the current node.

e. Move the current pointer to the next node.

4. Reset the head of the linked list.

3. Code Program

class Node {
    int data;
    Node prev;
    Node next;

    Node(int data) {
        this.data = data;
        prev = null;
        next = null;
    }
}

class DoublyLinkedList {
    Node head;

    // Function to reverse a Doubly Linked List
    void reverse() {
        Node temp = null;
        Node current = head;

        while (current != null) {
            temp = current.prev;
            current.prev = current.next;
            current.next = temp;
            current = current.prev;
        }

        if (temp != null) {
            head = temp.prev;
        }
    }

    // Insert a node at the beginning of the list
    void push(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        if (head != null) {
            head.prev = newNode;
        }
        head = newNode;
    }

    // Print the Doubly Linked List
    void printList(Node node) {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

    public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList();

        list.push(2);
        list.push(4);
        list.push(8);
        list.push(10);

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

        list.reverse();

        System.out.println("\nReversed Doubly linked list:");
        list.printList(list.head);
    }
}

Output:

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

4. Step By Step Explanation

1. A Node class is defined to create new nodes for the Doubly Linked List. Each node contains a data value, a prev pointer pointing to the previous node, and a next pointer pointing to the next node.

2. The DoublyLinkedList class contains methods to manipulate the linked list.

3. The reverse method is where the magic happens. It changes the next and prev pointers of each node toly reverse the linked list.

4. In the main function:

a. A DoublyLinkedList is created.

b. Nodes with data values 2, 4, 8, and 10 are added to the list.

c. The original list is printed.

d. The list is reversed.

e. The reversed list is printed.

5. The output shows the original doubly linked list and its reversed version.

Reversing a Doubly Linked List is a common operation in data structure manipulation, and understanding how pointers are switched is crucial to mastering the concept.