Iteratively Reverse a linked list using only 2 pointers (An Interesting Method) - GeeksforGeeks
Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
Given pointer to the head node of a linked list, the task is to reverse the linked list.
Examples:
Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL Input : Head of following linked list 1->2->3->4->5->NULL Output : Linked list should be changed to, 5->4->3->2->1->NULL
We strongly recommend you to minimize your browser and try this yourself first.
We have seen how to reverse a linked list in article Reverse a linked list. In iterative method we had used 3 pointers prev, cur and next. Below is an interesting approach that uses only two pointers. The idea is to use XOR to swap pointers.
- C/C++
- Python
C/C++
Read full article from Iteratively Reverse a linked list using only 2 pointers (An Interesting Method) - GeeksforGeeks
No comments:
Post a Comment