static int dividend=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
/**
* 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 == NULL)
return NULL;
if (head->next == NULL)
return head;
ListNode* prv = head;
ListNode* mid = head;
ListNode* last = head->next;
while(1){
if (prv != mid){
prv->next = mid->next;
mid->next = last->next;
last->next = mid;
}else{
mid->next = last->next;
last->next = mid;
head = last;
}
if (mid->next == NULL || mid ->next->next == NULL)
return head;
else{
prv = mid;
last = mid->next->next;
mid = mid->next;
}
}
}
};
LetCode 24. 两两交换链表中的节点
最新推荐文章于 2025-02-13 21:02:26 发布