Swap by using a dummy head, any improvement?


  • 0
    W
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head) {
            ListNode dummy(0);
            dummy.next = head;
            ListNode* cur = &dummy;
            while(cur && cur->next && cur->next->next){
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                temp->next = temp->next->next;
                cur->next->next = temp;
                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.