有空我觉得还得再写一遍。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
// 定义虚拟头节点
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp1 = cur->next;
ListNode* tmp2 = cur->next->next->next;
// 赋值
cur->next = cur->next->next;
cur->next->next = tmp1;
cur->next->next->next = tmp2;
// 后移
cur = cur->next->next;
}
return dummyHead->next;
}
};