-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path147.insertion-sort-list.js
67 lines (60 loc) · 1.37 KB
/
147.insertion-sort-list.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
/*
* @lc app=leetcode id=147 lang=javascript
*
* [147] Insertion Sort List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
// var insertionSortList = function(head) {
// if (head == null || head.next == null) {
// return head
// }
// let dummy = new ListNode(-Infinity)
// dummy.next = head // 插入一个节点
// let pre = dummy
// let cur = head.next // 从第二个开始
// while (cur != null) {
// while (pre.next != null && pre.next.val < cur.val) {
// pre = pre.next // 找到要插入的位置的前一个节点
// }
// let next = cur.next // 保存下个节点
// cur.next = pre.next
// pre.next = cur
// cur = next // 处理下个节点
// pre = dummy // 重设dummy
// }
// return dummy.next
// }
function insertionSortList(head) {
var before = { val: -Number.MAX_VALUE, next: null }
while (head) {
var prev = before
// find prev
while (prev.next && prev.next.val < head.val) {
prev = prev.next
}
var next = head.next
head.next = prev.next
prev.next = head
head = next
}
return before.next
}
// function ListNode(val) {
// this.val = val
// this.next = null
// }
// @lc code=end
module.exports = {
insertionSortList
}