How to reverse a linked list using recursion and iteration?
In the iteration approach we reverse linked list using 3 pointers in O(n) time, This is done by creating a new list by reversing direction, and subsequently inserting the element at the start of the list.
We can also reverse a singly linked list by using recursion. We will traverse the linked list until we find the tail, this tail would be the new head for reversed linked list.
-K Himaanshu Shuklaa..
In the iteration approach we reverse linked list using 3 pointers in O(n) time, This is done by creating a new list by reversing direction, and subsequently inserting the element at the start of the list.
We can also reverse a singly linked list by using recursion. We will traverse the linked list until we find the tail, this tail would be the new head for reversed linked list.
-K Himaanshu Shuklaa..
No comments:
Post a Comment