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 a linked list reverse-linked-list-example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR6bQuCoSwsyVXV-ytVWmP_aJwE9IFIujqhs9m9CLR2eFtYYFOjid9h3fxCusWC9GyHlReD9gnYFGyhXYXUy925yhm1GzEmNHPwjXkH5387tyweS9-fxDlXvVP2ZrrYPCl7PDQY0k-6RY/s640/ll_rev1.png)
No comments:
Post a Comment