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 5dd4175 commit 9acaf25Copy full SHA for 9acaf25
kotlin/0147-insertion-sort-list.kt
@@ -0,0 +1,29 @@
1
+class Solution {
2
+ fun insertionSortList(head: ListNode?): ListNode? {
3
+ val dummy = ListNode(0)
4
+ dummy.next = head
5
+ var prev = head
6
+ var cur = head?.next
7
+
8
+ while (cur != null) {
9
+ if (prev != null && cur.value >= prev.value) {
10
+ prev = cur
11
+ cur = cur.next
12
+ continue
13
+ }
14
15
+ var temp = dummy
16
+ while (cur.value > temp.next.value)
17
+ temp = temp.next
18
+ prev?.next = cur.next
19
+ cur.next = temp.next
20
+ temp.next = cur
21
+ cur = prev?.next
22
23
24
+ return dummy.next
25
26
27
+ val ListNode.value
28
+ get() = this.`val`
29
+}
0 commit comments