-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path148.sort-list.kt
More file actions
32 lines (32 loc) · 950 Bytes
/
Copy path148.sort-list.kt
File metadata and controls
32 lines (32 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution
// 归并排序,把链表拆成两半,各自排序,最后合并两个已排序的链表
fun Solution.sortList(head: ListNode?): ListNode? {
// 单元素
if(head?.next==null) return head
var slow:ListNode? = ListNode()
slow?.next = head
var fast = slow
while(fast?.next!=null){
slow = slow?.next
fast = fast?.next.next
}
val next = slow?.next
slow?.next = null
var left = sortList(head)
var right = sortList(next)
// 合并两个有序链表
val dumy = ListNode()
var current = dumy
while(left!=null && right!=null){
val node = if(left.`val`<right.`val`) left else right
val next = node.next
node.next = null
current.next = node
current = node
if(node==left) left = next
else right = next
}
if(left!=null) current.next = left
if(right!=null) current.next = right
return dumy.next
}