[C] simple solution in recursive way. O(n)


  • 0
    P

    A simple C solution in recursive way.

    struct ListNode* swapPairs(struct ListNode* head) {
        struct ListNode* tmp;
        if (!head)
            return NULL;
        if (!head->next)
            return head;
    
        tmp = head->next;
        head->next = swapPairs(head->next->next);
        tmp->next=head;
        
        return tmp;
    }
    

Log in to reply
 

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