Easy Understand C++ Code, make a swap function.


  • 0
       class Solution {
    public:
        ListNode * swap(ListNode *A, ListNode *B)
        {
            if(!B) return A;
            A->next=B->next;
            B->next=A;
            return B;
        }
        ListNode* swapPairs(ListNode* head) {
            ListNode * dom=new ListNode(0);
            dom->next=head;
            ListNode *start=dom;
            while(start && start->next)
            {
                start->next=swap(start->next, start->next->next);
                start=start->next->next;
            }
            return dom->next;
        }
    };

Log in to reply
 

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