// 解题思路:
// 虚拟头节点 + 节点指针两两交换。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == nullptr)
return nullptr;
auto dummy = new ListNode (-1);
dummy->next = head;
auto cur = dummy;
auto p1 = head, p2 = p1->next;
while(p1 != nullptr && p2 != nullptr) {
auto next = p2->next;
cur->next = p2;
p2->next = p1;
p1->next = next;
cur = p1;
p1 = cur->next;
if (p1 != nullptr)
p2 = p1->next;
}
return dummy->next;
}
};