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