C++ clean code


  • 0
    N
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head) {
            ListNode * dummy = new ListNode(0), *prev = dummy;
            ListNode * p = head, * q;
            dummy->next = head;
            while (p && p->next) {
                q = p->next;
    
                prev->next = q;
                p->next = q->next;
                q->next = p;
                
                prev = p;
                p = p->next;
            }
            
            return dummy->next;
        }
    };
    

Log in to reply
 

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