/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* swapLinkedPair(ListNode* head) {
if(!head || !head->next)return head;
ListNode dummy(0);
dummy.next = head;
ListNode *node = dummy.next->next;
ListNode *currhead = &dummy;
while(node){
//jiaohuan
currhead->next->next = node->next;
node->next = currhead->next;
currhead->next = node;
currhead = currhead->next->next; //更新下一组的头节点;
node = currhead; //更新用于检测节点,也是交换节点;
if(node->next){ //在运行时检测是否有两个成一组
node = node->next;
}else{
break;
}
node = node->next;
}
return dummy.next;
}
};