reversing linked list

后端 未结 9 1134
星月不相逢
星月不相逢 2020-12-10 16:52

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         


        
相关标签:
9条回答
  • 2020-12-10 17:26

    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;
    
    0 讨论(0)
  • 2020-12-10 17:26

    Some problems I could see:

    • You need to make the next pointer of the new last node NULL.
    • You existing function will blow if I pass NULL to it initially.
    0 讨论(0)
  • 2020-12-10 17:28

    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.

    0 讨论(0)
提交回复
热议问题