/**
* 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* newHead = new ListNode(0, head);
ListNode *p = newHead, *first = head, *second;
while(first && first->next) {
second = first->next;
first->next = second->next;
second->next = first;
p->next = second;
p = first;
first = p->next;
}
return newHead->next;
}
};