We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3a1ad4a commit 713648bCopy full SHA for 713648b
0147-insertion-sort-list/0147-insertion-sort-list.ts
@@ -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