Python solution O(nklog(k)): 179ms


  • 0
    B

    import heapq as p;

    class Solution(object):

    def mergeKLists(self, lists):
    
       heap = []
       for i in range(len(lists)):
            if lists[i]:
                p.heappush(heap, (lists[i].val, i))
                lists[i] = lists[i].next
    
        ret = cur = ListNode(-1)
        while heap:
            n_val, i = p.heappop(heap)
            cur.next = ListNode(n_val)
            if lists[i]:
                p.heappush(heap, (lists[i].val, i))
                lists[i] = lists[i].next
            cur = cur.next
            
        return ret.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.