/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head||!head->next) return head;
auto dummy=new ListNode(-1,head);
for(auto pre=dummy;pre->next&&pre->next->next;){
auto a=pre->next;
auto b=pre->next->next;
pre->next=b;
a->next=b->next;
b->next=a;
pre=a;
}
return dummy->next;
}
};
24. 两两交换链表中的节点
于 2024-03-17 15:10:03 首次发布