Reversing a linkedlist recursively in c
The general recursive algorithm for this is:
Divide
the list in2
parts - first node and rest of the list.- Recursively call reverse for the
rest
of the linked list. - Link
rest
tofirst
. - Fix
head
pointer
Here is the code with inline comments:
struct node* recursiveReverseLL(struct node* first){
if(first == NULL) return NULL; // list does not exist.
if(first->link == NULL) return first; // list with only one node.
struct node* rest = recursiveReverseLL(first->link); // recursive call on rest.
first->link->link = first; // make first; link to the last node in the reversed rest.
first->link = NULL; // since first is the new last, make its link NULL.
return rest; // rest now points to the head of the reversed list.
}
I hope this picture will make things clearer:
(source: geeksforgeeks.org)
.
Alternate solution :
struct node *head;
void reverse(struct node *prev, struct node *cur)
{
if(cur){
reverse(cur,cur->link);
cur->link = prev;
}
else{
head = prev;
}
}
In main, call reverse(NULL,head);
Let the linked list be 1-> 2 -> 3 ->4
the function in c is--
struct linked_node * reverse_recursive(struct linked_node * head)
{
struct linked_node * first;/*stores the address of first node of the linked
list passed to function*/
struct linked_node * second;/* stores the address of second node of the
linked list passed to function*/
struct linked_node * rev_head;/*stores the address of last node of initial
linked list. It also becomes the head of the reversed linked list.*/
//initalizing first and second
first=head;
second=head->next;
//if the linked list is empty then returns null
if(first=NULL)
return(NULL);
/* if the linked list passed to function contains just 1 element, then pass
address of that element*/
if(second==NULL)
return(first);
/*In the linked list passed to function, make the next of first element
NULL. It will eventually (after all the recursive calls ) make the
next of first element of the initial linked list NULL.*/
first->next=NULL;
/* storing the address of the reverse head which will be passed to it by the
condition if(second==NULL) hence it will store the address of last element
when this statement is executed for the last time. Also here we assume that
the reverse function will yield the reverse of the rest of the linked
list.*/
rev_head=reverse(second);
/*making the rest of the linked list point to the first element. i.e.
reversing the list.*/
second->next=first;
/*returning the reverse head (address of last element of initial linked
list) . This condition executes only if the initial list is 1- not empty
2- contains more than one element. So it just relays the value of last
element to higher recursive calls. */
return(rev_head);
}
now running the function for the linked list 1-> 2-> 3 -> 4
- inside reverse(&1) the code runs until rev_head=reverse(&2); // here &1 is address of 1.
list of function is
1(first)->2(second) -> 3 -> 4
inside reverse(&2) code runs until rev_head=reverse(&3); list of function
2(first)->3 (second)-> 4inside reverse(&3) code runs until rev_head=reverse (&4); list if function
3(first)-> 4 (second)inside reverse(&4) terminating condition second==NULL is true so return is executed and address of 4 is returned.
list of function
4(first)-> NULL(second)
- back to reverse(&3)
list of function is
NULL<-3(first) 4 (second)
and the value of rev_head=&4 which was returned
after executing second->next=first; list becomes
NULL<- 3(first) <-4 (second)
return (rev_head ); is executed which passes &4 because rev_head=&4
- back to rev(&2)
list in function is
NULL<-2(first) 3(second)<-4
and rev_head is &4 which was returned by rev(&3)
after executing second->next=first , list becomes
NULL<-2(first)<-3(second)<-4
return(rev_head); is executed which returns &4 to rev(&1);
- back to rev(&1)
list in function is
NULL<-1(first) 2(second)<-3<-4
and value of rev_head is &4 which was passed by reverse(&3)
now second->next =first is executed and list becomes
NULL<-1(first) <- 2(second)<-3<-4
return(rev_head); is executed // rev_head=&4 which was returned by reverse(&2) and the value of rev_head is passesd to the main function.
hope this helps. It took me quite a lot of time to understand this and also to write this answer.
/* Reverses a linked list, returns head of reversed list
*/
NodePtr reverseList(NodePtr curr) {
if (curr == NULL || curr->next == NULL) return curr; // empty or single element case
NodePtr nextElement = curr->next;
curr->next = NULL;
NodePtr head = reverseList(nextElement);
nextElement->next = curr;
return head;
}