class Solution { public: ListNode* swapPairs(ListNode* head) { ListNode* res=NULL; if(!head) return res; ListNode* front=head,*back=head->next,*n,*p=new ListNode(0),*h=back; while(back){ p->next=back; n=back->next;back->next=front; front->next=n; p=front; front=n; if(n)back=front->next; else break; } return h?h:head; } };
需要注意的是用p用来保存之前交换过的前链表