leetcode swap nodes in pairs

class Solution {
public:
    ListNode *swapPairs(ListNode *head) 
    {
        if(head==NULL)return NULL;
        ListNode *p=head,*ppre=head,*pnext;
        while(p&&p->next)
        {
            pnext=p->next;
            if(pnext)
            {
                if(ppre==head)head=pnext;
                else ppre->next=pnext;               
                p->next=pnext->next;
                pnext->next=p;
                ppre=p;
                p=p->next;
            }                      
        }
        return head;        
    }
};

 

posted @ 2013-09-04 12:01  代码改变未来  阅读(180)  评论(0编辑  收藏  举报