

class Solution {
public:
ListNode* swapPairs(ListNode* head)
{
if(head == nullptr || head->next == nullptr) return head;//0or1个节点
ListNode* newhead = new ListNode();
ListNode* tail = newhead;
ListNode* cur1 = head;
ListNode* cur2 = head->next;
ListNode* nnext = cur2->next;
while(cur1!=nullptr && cur2!=nullptr)
{
tail->next = cur2;
cur2->next = cur1;
cur1->next = nnext;
tail = cur1;
cur1=cur1->next;
if(cur1 == nullptr) break;
else cur2 = cur1->next;
if(cur2 != nullptr)
nnext = cur2->next;
}
return newhead->next;
}
};