Reversing a Linked List
Iterative and recursive approaches to reverse a linked list.
Reversing by Changing Node Data & Using an Array :
Pass the pointer to head node and an array as parameters.
Using a while loop, copy the data from each node to the array until the pointer becomes NULL.
Set the pointer to head and decrement to index i by 1 to reach the last element of array.
Overwrite the data in each node using a while loop and keep decrementing the index i until pointer becomes NULL.
Reversing using Sliding Pointers :
Take two additional pointers, prev and next, and initialise them with NULL.
Copy the next address of each node to the next pointer and then set the next address of node to the prev pointer.
Make current as next and prev as current in order to move forward.
Do this procedure until current becomes NULL.
Set the head node as prev pointer.
Reversing by Recursion :
Contributed by Nitin Ranganath
Last updated