/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* swapPairs(ListNode* head) {
ListNode hair(0), *cur = &hair;
hair.next = head;
while (cur->next && cur->next->next) {
ListNode* cur1 = cur->next;
ListNode* cur2 = cur1->next;
cur->next = cur2;
cur1->next = cur2->next;
cur2->next = cur1;
cur = cur1;
}
return hair.next;
}
};