My clear cpp code


  • -1
    C
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head) 
        {
            ListNode fake(0);
            ListNode* ptr = &fake;
            
            while(head && head->next)
            {
                ListNode* tmp = head->next->next;
                
                ptr->next = head->next;
                ptr = ptr->next;
                ptr->next = head;
                ptr = ptr->next;
                
                head = tmp;
            }
            
            ptr->next = head;
            return fake.next;
        }
    };

Log in to reply
 

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