Segmentation fault in a function to reverse a singly linked list recursivley.

Posted by Amanda on Stack Overflow See other posts from Stack Overflow or by Amanda
Published on 2010-04-12T09:48:41Z Indexed on 2010/04/12 11:33 UTC
Read the original article Hit count: 143

I am implementing a function to recursively reverse a linked-list, but getting seg-fault.

typedef struct _node {
   int data;
   struct _node *next;
} Node, *NodeP;

NodeP recursiveReverseList(NodeP first){
   if(first == NULL) return NULL;
   if(first->next == NULL) return first;

   NodeP rest = recursiveReverseList(first->next);
   rest->next = first;
   first->next = NULL;

   return first;
}

Can you please help?

P.S. The iterative version is working fine though. Its not homework. Just practicing C.

Thank you all :)

© Stack Overflow or respective owner

Related posts about c

    Related posts about recursion