/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/classSolution{public:ListNode*swapPairsStep(ListNode*head,intPairsNum){ListNode*now=head;ListNode*before;for(inti=0;i<PairsNum;i++){now=now->next;before=now;now=now->next;}if(PairsNum==0){ListNode*tmp=now->next;now->next=now->next->next;tmp->next=now;returntmp;}else{ListNode*tmp=now->next;now->next=now->next->next;tmp->next=now;before->next=tmp;}returnhead;}boolIsCheck(ListNode*head,intPairsNum){ListNode*now=head;for(inti=0;i<PairsNum;i++){now=now->next;now=now->next;}if(now!=NULL&&now->next!=NULL){returntrue;}else{returnfalse;}}ListNode*swapPairs(ListNode*head){// Note: The Solution object is instantiated only once and is reused by each test case.
intnum=0;while(IsCheck(head,num)){head=swapPairsStep(head,num);num++;}returnhead;}};