-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort_List.cc
More file actions
106 lines (94 loc) · 2.06 KB
/
Copy pathMerge_Sort_List.cc
File metadata and controls
106 lines (94 loc) · 2.06 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *sortList(ListNode *head);
};
ListNode* Solution::sortList(ListNode *head) {
if (head == NULL) return head;
ListNode *left = head;
ListNode *fast = head;
ListNode *slow = head;
ListNode *preslow = NULL;
while (fast != NULL) {
if (fast->next == NULL) break;
if (preslow == NULL) preslow = head;
else preslow = preslow->next;
slow = slow->next;
fast = fast->next->next;
}
//if (fast->next != NULL) slow = slow->next;
ListNode *right = slow;
if (left->next == right && right->next == NULL) {
if (left->val > right->val) {
right->next = left;
left->next = NULL;
return right;
} else return left;
} else if (left == right) return left;
preslow->next = NULL;
ListNode *l = this->sortList(left);
ListNode *r = this->sortList(right);
ListNode *i, *j, *ci, *pj = NULL;
for (i = r; i != NULL; ) {
ci = i;
i = i->next;
pj = NULL;
for (j = l; j != NULL; j = j->next) {
if (ci->val < j->val) {
if (j == l) {
ci->next = j;
l = ci;
} else {
ci->next = j;
pj->next = ci;
}
break;
} else {
if (pj == NULL) pj = j;
else pj = pj->next;
if (j->next == NULL) {
j->next = ci;
return l;
}
}
}
}
return l;
}
int main(int argc, char **argv) {
Solution s;
ListNode n1(5);
ListNode n2(2);
ListNode n3(1);
ListNode n4(4);
ListNode n5(12);
ListNode n6(9);
ListNode n7(100);
ListNode n8(5);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
ListNode *cur = &n1;
while (cur != NULL) {
cout << cur->val << endl;
cur = cur->next;
}
cout << "--------------" << endl;
ListNode *head = s.sortList(&n1);
cur = head;
while (cur != NULL) {
cout << cur->val << endl;
cur = cur->next;
}
}