Save my name, email, and website in this browser for the next time I comment. Problem: To test both data structure and the recursion concept, a wonderful and confusing interview question asked to experienced people is “Reverse a linked list using recursion”.. It divides the list into two parts first node and rest of the list, and then link rest to head in reverse order. Iterative; Recursive; 1. From the current situation with a partially reversed linked list, we can achieve a fully reversed linked list with the following operations: Reverse a singly linked list. Assume that the rest of the list had already been reversed by Recursion, now how do I reverse the front… The recursive version of this problem is slightly trickier and the key is to work backwards. There can be two ways to create the program, one is iterative and the other one is recursive. Solution:. Hot Network Questions Probability after n steps C Linked List Code Style Limit if the limit is a function Is this a Phishing attempt? Now let’s create a program for reversing the linked list. Now we need to do the rest of the work to have the whole linked list reversed. In this solution, we need 3 pointers. This problem can be solved in 2 ways. So we iterate and reverse the list until the current is null. It then recursively applies the same division until it reaches the last node, at that point whole linked list, is reversed. But must stop just before the end of the node, and we should have total 3 nodes in hand. Why did Marty McFly need to look up Doc Brown's address in 1955? First, to make a recursive algorithm, we need to understand how the present data in the list is being held. Recursively reverse linked list. Working with the recursive assumption. Program for Reverse a linked list (tail-recursive approach) Example. Live Demo This assumption that reverse() works for a subset of our linked list (but not the whole thing) has gotten us pretty far. So in every loop, we require a temporary pointer variable to save the start location while the list is being reversed. To reverse the linked list, we only need to reverse the pointers directions of the list. In recursive approach, we need to move to the end of the node. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL. Iterative solution. And initialize as shown below: prev = NULL Curr = head Temp = NULL.