23. Merge k Sorted Lists (Hard) - TengnanYao/daily_leetcode GitHub Wiki

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        heap = []
        for node in lists:
            if node:
                heapq.heappush(heap, (node.val, node))
        dummy = cur = ListNode()
        while heap:
            cur.next = heapq.heappop(heap)[1]
            cur = cur.next
            if cur.next:
                heapq.heappush(heap, (cur.next.val, cur.next))
        return dummy.next