sharing my C++ neat solution


  • 0
    B
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head) {
            ListNode *dummy = new ListNode(0), *cur = dummy;
            dummy->next = head;
            while(cur->next && cur->next->next) {
                ListNode* tmp = cur->next->next->next;
                cur->next->next->next = cur->next;
                cur->next = cur->next->next;
                cur->next->next->next = tmp;
                cur = cur->next->next;
            }
            return dummy->next;
        }
    };
    

Log in to reply
 

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