class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head) return nullptr;
ListNode* dummyHead = new ListNode();
dummyHead->next = head;
ListNode* pre = dummyHead;
ListNode* cur = head;
ListNode* next = cur->next;
while(cur && next){
//交换
pre->next = next;
cur->next = next->next;
next->next = cur;
//交换过后 next在前 pre在后
if(cur->next && cur->next->next){
pre = cur;
cur = cur->next;
next = cur->next;
}else break;
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};