Simple 0ms iterative and recursive solutions in C


  • 6
    M
    struct ListNode* reverseList(struct ListNode* head) {
        if(head==NULL||head->next==NULL)
        return head;
        struct ListNode *root=reverseList(head->next);
        head->next->next=head;
        head->next=NULL;
        return root;
    }
    
    
    struct ListNode* reverseList(struct ListNode* head) {
        struct ListNode *prev=NULL,*next;
        while(head)
        {
            next=head->next;
            head->next=prev;
            prev=head;
            head=next;
        }
        return prev;
    }

  • 0
    X

    hi,could you explain the 1st method(Recursion)? I‘m confused. thx frist~:)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.