-
Notifications
You must be signed in to change notification settings - Fork 29
/
23.merge-k-sorted-lists.kt
72 lines (69 loc) · 1.47 KB
/
23.merge-k-sorted-lists.kt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode id=23 lang=kotlin
*
* [23] Merge k Sorted Lists
*
* https://leetcode.com/problems/merge-k-sorted-lists/description/
*
* algorithms
* Hard (39.49%)
* Likes: 4576
* Dislikes: 284
* Total Accepted: 634.6K
* Total Submissions: 1.6M
* Testcase Example: '[[1,4,5],[1,3,4],[2,6]]'
*
* Merge k sorted linked lists and return it as one sorted list. Analyze and
* describe its complexity.
*
* Example:
*
*
* Input:
* [
* 1->4->5,
* 1->3->4,
* 2->6
* ]
* Output: 1->1->2->3->4->4->5->6
*
*
*/
// @lc code=start
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun mergeKLists(lists: Array<ListNode?>): ListNode? {
val queue = PriorityQueue<ListNode>() { a, b -> a.`val`-b.`val` }
for (list in lists) {
if (list != null) {
queue.add(list)
}
}
var head: ListNode? = null
var last: ListNode? = null
while (queue.isNotEmpty()) {
val top = queue.peek()
queue.remove()
if(top?.next != null) {
queue.add(top.next)
}
if (head == null) {
head = top
last = top
} else {
last?.next = top
last = top
}
}
return head
}
}
// @lc code=end