leetcode24. Swap Nodes in Pairs
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
int step = 2;
ListNode *front = new ListNode(0);
front->next = head;
ListNode *tail = front;
ListNode *lHead = front;
if(head==NULL){
return NULL;
}
if(head->next==NULL){
return head;
}
while(tail->next!=NULL){
tail = tail->next;
if(step<=1){
front->next->next = tail->next;
tail->next = front->next;
front->next = tail;
tail = tail->next;
front = tail;
step = 2;
}else{
step--;
}
}
return lHead->next;
}
};