I am trying to reverse a linked list using recursion and wrote the following code for it. The list is start of the list at the beginning.
node *reverse_lis
After the line current = reverse_list_recursive(current);
you are storing the new list head in current, so current->next = parent;
is wrong. New current
is the new list head, but you need to access the new list tail, i.e., the OLD current
:
node* newhead = reverse_list_recursive(current);
current->next = parent;
printf("\n %d %d \n",current->value,parent->value);
return newhead;
Some problems I could see:
When you reach the end of the list, you return the last node. That last node's next value then gets assigned to itself, hence you'd create an inconsistency. If current is NULL, return NULL instead and simply disregard the rest of the code if the return is NULL.