-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148_sort_list.cc
86 lines (77 loc) · 1.82 KB
/
148_sort_list.cc
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
using namespace::std;
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* travel(ListNode* head) {
int n = 0;
ListNode* ptr = head;
while (ptr) {
++n;
ptr = ptr->next;
}
if (n == 0) {
return NULL;
} else if (n == 1) {
head->next = NULL;
return head;
} else if (n == 2) {
if (head->val > head->next->val)
swap(head->val, head->next->val);
head->next->next = NULL;
return head;
}
int half = n >> 1;
ListNode* head2 = head;
while (half-- > 1)
head2 = head2->next;
auto tmp = head2;
head2 = head2->next;
tmp->next = NULL;
auto p1 = travel(head);
auto p2 = travel(head2);
ListNode* new_head = NULL;
if (p1->val <= p2->val) {
new_head = p1;
p1 = p1->next;
} else {
new_head = p2;
p2 = p2->next;
}
auto p = new_head;
while (p1 && p2) {
if (p1->val <= p2->val) {
p->next = p1;
p1 = p1->next;
} else {
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
if (!p1)
p->next = p2;
else
p->next = p1;
return new_head;
}
ListNode* sortList(ListNode* head) {
return travel(head);
}
};
int main()
{
return 0;
}