I was trying to reverse the link list using recursion and somehow I did it?
But one think is bothering me how the head in the last line finally points to the element 4 (i.e. the first element after reversing the link list if (1,2,3,4) was the initial link list?
What I am thinking it is still pointing to 2 after the reverse got complete but my programe is running correctly ? that sucks me?
Or am I logically wrong somewhere?
void Reverse(struct node* head)
{
struct node* f;
struct node* r;
/* empty list */
if (*head == NULL)
return;
/* suppose f = {1, 2, 3}, r = {2, 3} */
f = *head;
r = f->next;
/* List has only one node */
if (r == NULL)
return;
/* put the f element on the end of the list */
Reverse(&r);
f->next->next = f;
/* tricky step -- see the diagram */
f->next = NULL;
/* fix the head pointer */
*head = r;
}
Reverse(struct node** head)
. – Loki Astari Sep 13 '12 at 20:23