1class Solution:
2 def mergeKLists(self, lists: List[ListNode]) -> ListNode:
3
4 setattr(ListNode, "__lt__", lambda self, other: self.val <= other.val)
5
6 pq = []
7 for l in lists:
8 if l:
9 heapq.heappush(pq, l)
10
11 out = ListNode(None)
12 head = out
13 while pq:
14 l = heapq.heappop(pq)
15 head.next = l
16 head = head.next
17 if l and l.next:
18 heapq.heappush( pq, l.next)
19
20 return out.next
21