Reverse Double Linked List

Given a double linked list’s head node, reverse the list and return the new head node.

Java Solution

/*
    * For your reference:
    *
    * DoublyLinkedListNode {
    *     int data;
    *     DoublyLinkedListNode next;
    *     DoublyLinkedListNode prev;
    * }
    *
    */
static DoublyLinkedListNode reverse(DoublyLinkedListNode head) {
    DoublyLinkedListNode p = head;
    DoublyLinkedListNode newHead = head;
 
    while(p!=null){
        DoublyLinkedListNode t = p.next;
        p.next = p.prev;
        p.prev = t;
 
        newHead= p;
        p = t;
    }
 
    return newHead;
}

Leave a Comment