思路还是比较清晰的
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==nullptr) return nullptr;
ListNode *res=new ListNode(0);
res->next=head;
ListNode *pre=res;
ListNode *cur=head;
while(cur!=nullptr&&cur->next!=nullptr){
pre->next=cur->next;
cur->next=cur->next->next;
pre->next->next=cur;
pre=cur;
cur=cur->next;
}
return res->next;
}
};