Reversing a linkedlist recursively in c

后端 未结 9 1262
滥情空心
滥情空心 2020-12-07 23:33

The following code works fine when head is sent as a parameter to it. As I am new to C, I couldn\'t understand how it works. Help me out please.

struct node          


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 23:50

        To fix head also:
    
    void reverse_list_recursive_internal (struct list **head, struct list *node)
    {
        /* last node, fix the head */
        if (node->next == NULL) {
            *head = node;
            return; 
        }
        reverse_list_recursive_internal(head, node->next);
        node->next->next = node;
        node->next = NULL;
    }
    
    void reverse_list_recursive (struct list **head)
    {
        if (*head == NULL) {
            return;
        }
        reverse_list_recursive_internal(head, *head);
    }
    

提交回复
热议问题