Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Thursday, May 11, 2017

Reverse Doubly Linked list without extra space


Here i will swap next and previous values of all nodes and we will start traversing from the last, i.e. last node is now start node


/*
  class Node {
     int data;
     Node next;
     Node prev;
  }
*/

Node Reverse(Node head) {
Node temp=head;
    Node switcher;
    while(temp!=null)
        {
        switcher=temp.next;
        temp.next=temp.prev;
        temp.prev=switcher;
        head=temp;
        temp=temp.prev;
       
    }
    return head;
}

0 Comments:

Post a Comment

Stats