forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
34 lines (34 loc) · 814 Bytes
/
Solution.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
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var insertionSortList = function (head) {
if (head == null || head.next == null) return head;
let dummy = new ListNode(head.val, head);
let prev = dummy,
cur = head;
while (cur != null) {
if (prev.val <= cur.val) {
prev = cur;
cur = cur.next;
continue;
}
let p = dummy;
while (p.next.val <= cur.val) {
p = p.next;
}
let t = cur.next;
cur.next = p.next;
p.next = cur;
prev.next = t;
cur = t;
}
return dummy.next;
};