本文共 872 字,大约阅读时间需要 2 分钟。
class Solution { public: ListNode* swapPairs(ListNode* head) { if(!head||!head->next) return head; ListNode *headptr=new ListNode(0); headptr->next=head; ListNode* pre; ListNode *before; ListNode *after; ListNode *nextpair; pre=headptr; before=head; after=head->next; while(after!=NULL&&before!=NULL) { nextpair=after->next; after->next=before; pre->next=after; pre=before; if(nextpair!=NULL) { before=nextpair; after=nextpair->next; if(!after) { pre->next=before; } } else { before->next=NULL; break; } } return headptr->next; }};
转载地址:http://shked.baihongyu.com/