forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
142 lines (114 loc) · 3.45 KB
/
main2.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2018-08-21
#include <iostream>
#include <vector>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class ListOp{
public:
// 根据n个元素的数组arr创建一个链表, 并返回链表的头
static ListNode* createLinkedList(const vector<int>& arr){
if(arr.size() == 0)
return NULL;
ListNode* head = new ListNode(arr[0]);
ListNode* curNode = head;
for(int i = 1 ; i < arr.size() ; i ++){
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}
return head;
}
// 打印以head为头结点的链表信息内容
static void printLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode){
cout << curNode->val << " -> ";
curNode = curNode->next;
}
cout << "NULL" << endl;
}
// 释放以head为头结点的链表空间
static void deleteLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode){
ListNode* delNode = curNode;
curNode = curNode->next;
delete delNode;
}
}
};
/// Merge Sort Bottom Up
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
ListNode* sortList(ListNode* head) {
ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;
int sz = 1;
while(true){
ListNode *pre = dummyHead, *cur = pre;
while(cur->next){
cur = pre;
for(int i = 0; i < sz && cur->next; i ++, cur = cur->next);
if(cur->next){
ListNode* pre2 = cur;
for(int i = 0; i < sz && cur->next; i ++, cur = cur->next);
ListNode* next = cur->next, *head2 = pre2->next;
pre2->next = NULL, cur->next = NULL;
ListNode* tail;
pre->next = merge(pre->next, head2, tail);
pre = tail;
pre->next = next;
}
else if(pre == dummyHead){
sz = 0;
break;
}
}
if(sz == 0) break;
else sz *= 2;
}
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
private:
ListNode* merge(ListNode* a, ListNode* b, ListNode* &tail){
ListNode* dummyHead = new ListNode(-1);
ListNode *p1 = a, *p2 = b, *p = dummyHead;
while(p1 && p2)
if(p1->val < p2->val){
p->next = p1;
p1 = p1->next;
p = p->next;
p->next = NULL;
}
else{
p->next = p2;
p2 = p2->next;
p = p->next;
p->next = NULL;
}
if(p1) p->next = p1;
if(p2) p->next = p2;
tail = p;
while(tail->next) tail = tail->next;
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
int main() {
vector<int> arr = {4, 2, 1, 3};
ListNode* head = Solution().sortList(ListOp::createLinkedList(arr));
ListOp::printLinkedList(head);
ListOp::deleteLinkedList(head);
return 0;
}