Share my 2ms c code


  • 0
    Z
    struct ListNode* swapPairs(struct ListNode* head) {
    if (head==NULL || head->next==NULL) return head;
    struct ListNode *p1, *p2, *p3;
    p1=head;
    head=head->next;
    while ((p1)&&(p1->next)){
        p2=p1->next;
        p3=p1->next->next;
        p2->next=p1;
        if (p3 && (p3->next)) p1->next=p3->next;
        else p1->next=p3;
        p1=p3;
    }
    return head;
    

    }


Log in to reply
 

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