-
Notifications
You must be signed in to change notification settings - Fork 0
/
147.cpp
86 lines (81 loc) · 2.54 KB
/
147.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//通过修改值来实现,但也非常难,因为是链表而非数组
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
for (ListNode *mark = head->next; mark != nullptr; mark = mark->next) {
int val = mark->val;
ListNode *run = head;
while (run != mark && run->val <= val) {//找到前面已排序结点里大于val的最小值
run = run->next;
}
if (run != mark) {
ListNode *temp =run;//保留数字
int x; //用两个变量来向右移动链表的值(只能这么做)
int y = run->val;
while (run != mark) {
x = y;
y = run->next->val;
run->next->val = x;
run = run->next;
}
temp->val = val;
}
}
return head;
}
};
/*class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
for (ListNode *mark = head->next; mark != nullptr; mark = mark_next) {
int val = mark->val;
ListNode *run = head;
ListNode *mark_next = mark->next;
while (run->next != mark && run->next->val <= val) {//找到前面已排序结点里大于val的最小值
run = run->next;
}
if (run->next != mark) {
mark->next = run->next;
run->next = mark;
temp->val = val;
}
}
return head;
}
};*/
/*class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
ListNode *sortedHead = new ListNode(-1);
while(head != NULL)
{
//保存head位置
ListNode *temp = head->next;
ListNode *cur = sortedHead;
while(cur->next != NULL && cur->next->val < head->val)
{
cur = cur->next;
}
//插入
head->next = cur->next;
cur->next = head;
//恢复head
head = temp;
}
return sortedHead->next;
}
};*/