void reverse(LIST **head)
{
if(!*head)
return ;
LIST *first=*head,*rest=(*head)->next;
if(!rest)
return;
reverse(&rest);
first->next->next = first;
first->next= NULL;
*head = rest;
// printf(" :%d",rest->data);
}
This program is working. The mentioned recursive code is for reversing the singly linked list.Consider a list L={1,2,3,4,5} as input.Consider two cases, case 1 if we uncomment statement 10, output will be data of last node i.e. 5 four times, case 2 if we comment statement no. 09 then the printf will print 5,4,3,2. My question is, in case 1 due to this statement *head=rest; why we get constant value for rest->data each call of function? If we removed statement no. 09 then printf will print different values of rest->data.
Thank you so much in advance.
Here is the answer! :-)
What is happening here is every time function call is returned, "*head = rest;" this statement is updating the value at location *head(which is address of head pointer) with address of rest pointer, which has effect throughout in program execution context. Every time function call returns head pointer is updated, means in every previous call rest pointer is updated (refer line 6 comment).