LeetCode 24
https://leetcode.cn/problems/swap-nodes-in-pairs/description/
难度:中等
高频面试题汇总:https://www.yuweihung.com/posts/2025/lc-hot/
K 个一组反转链表的简单版本。
时间复杂度:O(n),其中 n 为链表长度。
空间复杂度:O(1)。仅用到若干额外变量。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummy = new ListNode(0, head);
ListNode* p0 = dummy;
ListNode* pre = nullptr;
ListNode* cur = p0->next;
while (cur && cur->next) {
for (int i = 0; i < 2; i++) {
ListNode* nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
ListNode* nxt = p0->next;
p0->next->next = cur;
p0->next = pre;
p0 = nxt;
}
return dummy->next;
}
};