-
Notifications
You must be signed in to change notification settings - Fork 481
/
0147.cpp
111 lines (107 loc) · 2.78 KB
/
0147.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
#include <iostream>
#include <climits>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
ListNode* insertionSortList(ListNode* head)
{
ListNode* dummy = new ListNode(INT_MIN);
for (ListNode* curr = head; curr != nullptr;)
{
ListNode* pre = findInsertPoint(dummy, curr->val);
ListNode* tmp = curr->next;
curr->next = pre->next;
pre->next = curr;
curr = tmp;
}
return dummy->next;
}
private:
ListNode* findInsertPoint(ListNode* head, int val)
{
ListNode* pre = nullptr;
for (ListNode* curr = head; curr != nullptr && curr->val <= val; pre = curr, curr = curr->next) {
}
return pre;
}
};
class Solution {
public:
ListNode* insertionSortList(ListNode* head)
{
ListNode* new_head = new ListNode(0);
new_head -> next = head;
ListNode* pre = new_head;
ListNode* cur = head;
while (cur)
{
if (cur -> next && cur -> next -> val < cur -> val)
{
while (pre -> next && pre -> next -> val < cur -> next -> val)
pre = pre -> next;
/* Insert cur -> next after pre.*/
ListNode* temp = pre -> next;
pre -> next = cur -> next;
cur -> next = cur -> next -> next;
pre -> next -> next = temp;
/* Move pre back to new_head. */
pre = new_head;
}
else cur = cur -> next;
}
ListNode* res = new_head -> next;
delete new_head;
return res;
}
};
class Solution
{
public:
ListNode *insertionSortList(ListNode *head)
{
if (head == nullptr || head->next == nullptr)
{
return head;
}
ListNode* h = new ListNode(-1);
h->next = head;
ListNode* cur = head;
ListNode* lat = cur->next;
while (lat != nullptr)
{
ListNode* tmp = h->next;
ListNode* pre = h;
while (tmp != lat && tmp->val < lat->val)
{
tmp = tmp->next;
pre = pre->next;
}
if (tmp == lat)
{
cur = lat;
}
else
{
cur->next = lat->next;
lat->next = tmp;
pre->next = lat;
}
lat = cur->next;
}
ListNode* retNode = h->next;
delete h;
return retNode;
}
};
int main()
{
return 0;
}