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;
}
/*
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