AcWing 3757. 重排链表
原题链接
中等
作者:
je5r1ta
,
2023-09-27 16:15:39
,
所有人可见
,
阅读 47
class Solution {
public:
void rearrangedList(ListNode* head) {
int n = 0;
auto p = head;
while (p) n++, p = p->next;
p = head;
for (int i = 0; i < n / 2; i++)
p = p->next;
// len(p) >= len(q)
auto q = p->next;
p->next = nullptr;
if (q) {
// reverse q
auto t = q->next;
q->next = nullptr;
while (t) {
auto tt = t->next;
t->next = q;
q = t;
t = tt;
}
}
// cross merge
p = head;
while (q) {
auto t = p->next;
p->next = q;
auto tt = q->next;
q->next = t;
q = tt;
p = t;
}
}
};
hh -> https://www.acwing.com/file_system/file/content/whole/index/content/10124490/