Skip to content

Commit f609dd7

Browse files
authored
Create 0086-partition-list.kt
1 parent db815cd commit f609dd7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

kotlin/0086-partition-list.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun partition(head: ListNode?, x: Int): ListNode? {
3+
var leftPart = ListNode(0)
4+
val rightPart = ListNode(0)
5+
6+
var cur = head
7+
var left = leftPart
8+
var right = rightPart
9+
10+
while (cur != null) {
11+
if (cur.`val` < x) {
12+
left?.next = cur
13+
left = left?.next
14+
} else {
15+
right?.next = cur
16+
right = right?.next
17+
}
18+
cur = cur?.next
19+
}
20+
21+
right?.next = null
22+
left?.next = rightPart?.next
23+
return leftPart?.next
24+
}
25+
}

0 commit comments

Comments
 (0)