Skip to content

Commit 713648b

Browse files
committed
Time: 151 ms (8.82%), Space: 45.8 MB (38.24%) - LeetHub
1 parent 3a1ad4a commit 713648b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function insertionSortList(head: ListNode | null): ListNode | null {
14+
let newHead = new ListNode(0)
15+
while(head){
16+
const t = head
17+
head = head.next
18+
let cur = newHead
19+
while(cur){
20+
if(!cur.next || t.val <= cur.next.val){
21+
[cur.next, t.next] = [t, cur.next]
22+
break
23+
}
24+
cur = cur.next
25+
}
26+
}
27+
return newHead.next
28+
};

0 commit comments

Comments
 (0)