Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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; 
} 
share

1 Answer

If you want to change the head pointer, you must pass it by reference (as a pointer). The prototype should be modified to receive the head as S **.

S *reverseRecursive(S **headref);
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.