-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
170 lines (165 loc) · 3.66 KB
/
index.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* @lc app=leetcode id=147 lang=javascript
*
* [147] Insertion Sort List
*
* https://leetcode.com/problems/insertion-sort-list/description/
*
* algorithms
* Medium (36.20%)
* Total Accepted: 167.4K
* Total Submissions: 426.8K
* Testcase Example: '[4,2,1,3]'
*
* Sort a linked list using insertion sort.
*
*
*
*
*
* A graphical example of insertion sort. The partial sorted list (black)
* initially contains only the first element in the list.
* With each iteration one element (red) is removed from the input data and
* inserted in-place into the sorted list
*
*
*
*
*
* Algorithm of Insertion Sort:
*
*
* Insertion sort iterates, consuming one input element each repetition, and
* growing a sorted output list.
* At each iteration, insertion sort removes one element from the input data,
* finds the location it belongs within the sorted list, and inserts it
* there.
* It repeats until no input elements remain.
*
*
*
* Example 1:
*
*
* Input: 4->2->1->3
* Output: 1->2->3->4
*
*
* Example 2:
*
*
* Input: -1->5->3->4->0
* Output: -1->0->3->4->5
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var insertionSortList = function(head) {
function getListNodeLength(head) {
let length = 0;
let cur = head;
while (cur) {
length++;
cur = cur.next;
}
return length
}
let length = getListNodeLength(head);
let curHead = head;
let insertedTmp = head.next;
let insertedIndex = 1;
function insertHelp(insertedIndex, insertedNode, insertedNodePre) {
let curIndex = 0;
let cur = curHead;
let curPreNode = null;
while (curIndex < insertedIndex) {
if (insertedNode.val < cur.val) {
if (insertedNodePre) {
insertedNodePre.next = insertedNode.next;
}
insertedNode.next = cur;
curIndex = insertedIndex;
if (curPreNode) {
curPreNode.next = insertedNode;
} else {
curHead = insertedNode;
}
curIndex = insertedIndex;
}
curPreNode = cur;
cur = cur.next;
curIndex++;
}
}
function getPreNode(index) {
let pre = null;
let curIndex = 0;
let cur = curHead;
while (curIndex < index) {
pre = cur;
cur = cur.next;
curIndex++;
}
return pre;
}
while (insertedIndex < length) {
const insertedPreNode = getPreNode(insertedIndex);
const next = insertedTmp.next;
insertHelp(insertedIndex, insertedTmp, insertedPreNode);
insertedTmp = next;
insertedIndex ++;
}
return curHead;
};
const { make_list_node, ListNode } = require('../utils');
const test1 = make_list_node([0, 1]);
/**
* 思路2
*
* 1. 先手工做一个空队列的指针
* 2. 遍历这个空队列,往这个空队列里插节点
*
* @param head
* @returns {ListNode}
*/
insertionSortList = function(head) {
if (!head || !head.next) return head;
const tmpHead = new ListNode(null);
let headNode = tmpHead;
let cur = head;
while (cur) {
const next = cur.next;
while (headNode.next && headNode.next.val < cur.val) {
headNode = headNode.next;
}
cur.next = headNode.next;
headNode.next = cur;
headNode = tmpHead;
cur = next;
}
return tmpHead.next;
}
printNodeList(insertionSortList(test1));
function printNodeList(head) {
let cur = head;
while(cur) {
console.log(cur.val);
cur = cur.next;
}
}
module.exports = {
id:'147',
title:'Insertion Sort List',
url:'https://leetcode.com/problems/insertion-sort-list/description/',
difficulty:'Medium',
}