How to reverse a link list without using another link list?
Note: Linked list is singly linked list (there is only next pointer no previous pointer).
See Solution: Reverse a link list without using another linked list
Take two pointers previous and current, increment both pointers by one till current reaches at the end of the list.
Node* reverse(Node* node) { if(node == null) return null; Node * prev = node; Node* curr = node->next; while(curr != null){ Node* tmp = curr->next; curr->next = prev; prev = curr; curr = temp; } return prev; }
Facebook Comments
Rahul says
awsum logic for reverse a linklist ..thankss
puzzlersworld says
Thanks Rahul