Im trying to understand the below code:
void recursiveReverse(struct node **head_ref)
{
struct node *first;
struct node *rest;
if (*head_ref == NULL)
return;
first = *head_ref;
rest = first->next;
if (rest == NULL)
return;
recursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}
I noticed that the variable rest is having the same value for all the recursion call once the code reached beyond the recursiveReverse(&rest). But first->next has different values. I was able to understand why first->next has different values by writing them on a stack and comparing it with each call. But i could not understand how rest is having the same value for all the calls instead of the (rest = first->next) from the stack. Please let me know if the question is not clear or if any details are needed.
Thanks
Update: I observed that, arranging the parameters properly, if i call recursivereverse(rest) instead of revursivereverse(&rest), the rest value changes for every recursive call just like any other variable on the revursion stack. I could not understand what is the difference &rest is making in the call.
recursiveReverse(&rest);is executed, the list is almost reversed. At that time,restpoints to the last item of the list, which is same regardless of the level of nesting in the recursion.