Skip to content

Commit 9acaf25

Browse files
authored
Create 0147-insertion-sort-list.kt
1 parent 5dd4175 commit 9acaf25

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

kotlin/0147-insertion-sort-list.kt

+29
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)