-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.h
39 lines (35 loc) · 877 Bytes
/
143.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Created by 17336 on 2022/3/28.
//
#ifndef HOT100_143_H
#define HOT100_143_H
#include <linknode.h>
class Solution {
public:
void reorderList(ListNode *head) {
ListNode *fast = head, *slow = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
}
ListNode *t = slow->next;
slow->next = nullptr;
slow = t;
ListNode *pre = nullptr;
while (slow) {
ListNode *temp = slow->next;
slow->next = pre;
pre = slow;
slow = temp;
}
ListNode *p = head;
while (p&&pre) {
ListNode *temp=pre->next;
pre->next = p->next;
p->next = pre;
p = pre->next;
pre = temp;
}
}
};
#endif //HOT100_143_H