/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if(head==NULL||head->next==NULL) return head;
ListNode *pre;
ListNode *cur;
pre=head->next;
cur=pre->next;
pre->next=head;
head->next=cur;
head=pre;
pre=pre->next;
while(pre!=NULL){
ListNode *post;
cur=pre->next;
if(cur!=NULL&&cur->next!=NULL) post=cur->next;
else break;
pre->next=post;
cur->next=post->next;
post->next=cur;
pre=cur;
}
return head;
}
};