Swap List Nodes in pairs

https://www.interviewbit.com/old/problems/swap-list-nodes-in-pairs/

ListNode* Solution::swapPairs(ListNode* A) {
    if (!A)
        return NULL;
    ListNode* head = A;
    ListNode* curr, *next, *prev;
    prev = NULL;
    curr = A;
    next = curr->next;
    if(next)
        A = next;
    while (next)
    {
        curr->next = next->next;
        next->next = curr;
        if(prev)            
            prev->next = next;      
        prev = curr;
        curr = curr->next;
        if(curr)
            next = curr->next;
        else
            next = NULL;
    }
    return A;
}

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!