My clean c++ code with pointer-to-pointer


  • 0
    T
    ListNode* swapPairs(ListNode* head) {
            ListNode **cur = &head;
            ListNode *entry = head;
            while (*cur)
            {
                entry = *cur;
                if (entry->next)
                {
                    *cur = entry->next;
                    entry->next = (*cur)->next;
                    (*cur)->next = entry;
                    cur = &(entry->next);
                }
                else
                {
                    break;
                }
            }
            return head;
        }

Log in to reply
 

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