Share 1ms C solution


  • 0

    more codes see: https://github.com/lightmen/leetcode.git

    struct ListNode* swapPairs(struct ListNode* head) {

    struct ListNode root;
    struct ListNode *prev,*first,*second,*tmp;
    root.next = head;
    prev = &root;
    first = head;
    while(first){
        second = first->next;
        if(second){
            tmp = second->next;
            prev->next = second;
            second->next = first;
            first->next = tmp;
            prev = first;
        }
        first = first->next;
    }
    return root.next;
    

    }


Log in to reply
 

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