Reversing a linkedlist recursively in c

后端 未结 9 1279
滥情空心
滥情空心 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-08 00:05

    Alternate solution :

    struct node *head;
    void reverse(struct node *prev, struct node *cur)
    {
       if(cur){
          reverse(cur,cur->link);
          cur->link = prev;
        }
        else{
          head = prev;
        }
    }
    

    In main, call reverse(NULL,head);

提交回复
热议问题