-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion-sort-list.js
More file actions
122 lines (109 loc) · 2.96 KB
/
insertion-sort-list.js
File metadata and controls
122 lines (109 loc) · 2.96 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
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
/**
* Source: https://leetcode.com/problems/insertion-sort-list/
* Tags: [Linked List,Sort]
* Level: Medium
* Title: Insertion Sort List
* Auther: @imcoddy
* Content: Sort a linked list using insertion sort.
*/
var util = require("./util.js");
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
/**
* Memo: Simply search each node and append it to list.
* Complex: O(n^2)
* Runtime: 235ms
* Tests: 21 test cases passed
* Rank: B
*/
var insertionSortList = function(head) {
var dummy = new ListNode(null);
var tail = dummy;
var p = head;
while (p) {
tail = dummy;
while (tail && tail.next && p.val > tail.next.val) {
tail = tail.next;
}
var q = p.next;
p.next = tail.next;
tail.next = p;
p = q;
}
return dummy.next;
};
/**
* Memo: The link list might contain some sub sorted list, so it will be faster to search the sub sorted list then append it as a whole.
* Complex: O(n^2)
* Runtime: 176ms
* Tests: 21 test cases passed
* Rank: A
*/
var insertionSortList = function(head) {
var dummy = new ListNode(null);
var pointer = dummy;
var start = head;
while (start) {
pointer = dummy;
while (pointer && pointer.next && start.val > pointer.next.val) {
pointer = pointer.next;
}
var next_val = Number.MAX_VALUE;
if (pointer && pointer.next) {
next_val = pointer.next.val;
}
var end = start;
var end_next = end.next;
while (end_next && end_next.val > end.val && end_next.val < next_val) {
end = end_next;
end_next = end.next;
}
end.next = pointer.next;
pointer.next = start;
start = end_next;
}
return dummy.next;
};
/**
* Memo: Create a new list using dummy head, and simply append each node to that list.
* Complex: O(n^2)
* Runtime: 232ms
* Tests: 21 test cases passed
* Rank: B
*/
var insertionSortList = function(head) {
var dummy = new ListNode(null);
var p = head;
while (p) {
var tail = dummy;
while (tail.next && tail.next.val < p.val) {
tail = tail.next;
}
var q = p.next;
p.next = tail.next;
tail.next = p;
p = q;
}
return dummy.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
var should = require('should');
console.time('Runtime');
console.timeEnd('Runtime');
util.lta(insertionSortList(util.atl([]))).should.eql([]);
util.lta(insertionSortList(util.atl([1]))).should.eql([1]);
util.lta(insertionSortList(util.atl([1, 1, 2, 2, 4]))).should.eql([1, 1, 2, 2, 4]);
util.lta(insertionSortList(util.atl([1, 3, 2, 5, 4]))).should.eql([1, 2, 3, 4, 5]);
util.lta(insertionSortList(util.atl([1, 20, 3, 2, 5, 4, 6, 7, 8]))).should.eql([1, 2, 3, 4, 5, 6, 7, 8, 20]);