注意只能换节点,不能换值。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *cur = head;
ListNode *preCur = NULL;
if(head&&head->next) head = head->next;
while(cur){
if(cur->next){
ListNode *tmp = cur->next->next;
cur->next->next = cur;
if(preCur) preCur->next = cur->next;
preCur = cur;
cur->next = tmp;
}
cur = cur->next;
}
return head;
}
};