Clear C code, 0 ms


  • 0
    M
    struct ListNode* swapPairs(struct ListNode* head) {
        struct ListNode *prev = NULL, *first = head, *second = NULL;
        for (; first && (second = first->next); prev = first, first = first->next) {
            first->next = second->next;
            second->next = first;
            if (prev) {
                prev->next = second;
            } else {
                head = second;
            }
        }
        return head;
    }

Log in to reply
 

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