-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSortList.cpp
More file actions
90 lines (81 loc) · 2.42 KB
/
InsertionSortList.cpp
File metadata and controls
90 lines (81 loc) · 2.42 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
/**
* 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 ((NULL == head) || (NULL == head->next))
return head;
ListNode *phead = new ListNode(INT_MIN); // fake head node
phead->next = head;
ListNode *pnode = head;
ListNode *nnode = head->next;
while (nnode)
{
// find a position from [phead, nnode] to insert nnode
ListNode *pnode2 = phead; // phead here, not head
ListNode *nnode2 = phead->next;
while ((nnode2 != nnode) && (nnode2->val <= nnode->val))
{
pnode2 = nnode2;
nnode2 = nnode2->next;
}
if (nnode2 == nnode) // dont need to insert
{
pnode = nnode;
nnode = nnode->next;
}
else
{
pnode->next = nnode->next; // delete nnode
pnode2->next = nnode; // insert nnode
nnode->next = nnode2;
nnode = pnode->next; // get next node
}
}
//pnode->next = NULL; // caution
return phead->next;
}
};
*/
// 和法1是一样的,练练手……
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
// foreach from head to tail to find the insertion place
if (!head || !(head->next))
return head;
ListNode dummy(INT_MIN);
dummy.next = head;
ListNode *pre = head;
ListNode *node = head->next;
while (node)
{
ListNode *next = node->next;
ListNode *node2 = &dummy; // foreach from head
while ((node2 != node) && (node2->next->val <= node->val))
node2 = node2->next;
if (node2 == node)
pre = node; // node stays still
else
{
node->next = node2->next;
node2->next = node;
pre->next = next; // pre stays still
}
node = next; // move on
}
return dummy.next;
}
};