题解:
1.头部会改变创建虚拟头结点
2.前两个点定义为a,b,c
3.p指向b,a指向c,b指向a ; p指向本段最后一个结点。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
auto dummy = new ListNode(-1);
dummy->next = head;
for (auto p = dummy; p->next && p->next->next;) {
auto a = p->next, b = a->next;
p->next = b;
a->next = b->next;
b->next = a;
p = a;
}
return dummy->next;
}
};