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

  • 0

    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);
        return tmp;

Log in to reply

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