I have the following code which I am using to return the reverse of the linked list . Though it reverses the linked list I never get the head of the reversed linked list. Because the restofElements node is getting over written. Any idea how can I get the head of the reversed linked list node returned to the calling program?
S *reverseRecursive(S *headref)
{
S *firstElement = NULL;
S *restOfElements = NULL;
if (headref==NULL)
{
return ;
}
firstElement = headref;
restOfElements = headref->next;
if (restOfElements == NULL)
return headref;
reverseRecursive(restOfElements);
firstElement->next->next = firstElement;
firstElement->next = NULL;
headref = restOfElements;
return headref;
}