-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
69 lines (65 loc) · 1.38 KB
/
program.cpp
File metadata and controls
69 lines (65 loc) · 1.38 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
#include "../include/pre.h"
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* makeListNode(const vector<int>& in)
{
auto p = new ListNode(0);
auto rst = p;
for (auto i : in)
{
auto pp = new ListNode(i);
p->next = pp;
p = pp;
}
return rst;
}
void insert(ListNode* cur, ListNode* node)
{
auto cp = cur->next;
cur->next = node;
node->next = cp;
}
ListNode* insertionSortList(ListNode* head)
{
auto insertSort = [&](ListNode* node){
auto cur = head;
ListNode* pri = nullptr;;
while (cur != nullptr && node->val > cur->val)
{
pri = cur;
cur = cur->next;
}
if (pri == nullptr) {
node->next = head;
head = node;
}
else {
insert(pri, node);
}
};
auto next = head->next;
head->next = nullptr;
while (next != nullptr)
{
std::cout << next->val << std::endl;
auto tmp = next->next;
insertSort(next);
next = tmp;
}
return head;
}
int main()
{
vector<int> vals = {70, 3, 4, 5, 623, 231, 45, 63};
auto rst = insertionSortList(makeListNode(vals));
while (rst != nullptr)
{
std::cout << rst->val << ", ";
rst = rst->next;
}
std::cout << std::endl;
return 0;
}