Reverse a linked list

For reversing a linked list we use 3 pointers ( prev , curr , next).Firstly we initialize prev with NULL,curr with head pointer and next with curr->next.Then each time we make curr's next pointer point to prev and then we advance the three pointers.We continue this process until curr becomes NULL.


Algorithm

prev = NULL , curr = head , next = curr->next

Until curr not NULL
      curr->next = prev;
      prev = curr;
      curr = next;
      next = curr->next


Example

reverse-linked-list-example



No comments:

Post a Comment