题目:https://leetcode.com/problems/swap-nodes-in-pairs/description/
代码:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL)
return NULL;
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *p=dummy,*q=head;
while(q!=NULL&&q->next!=NULL){
ListNode *temp = q->next->next;
q->next->next = q;
p->next = q->next;
q->next = temp;
p = q;
q = q ->next;
}
return dummy->next;
}
};