Below code works on my laptop. But it OJ doesn't accept it. It gives right result for the input [[1,1,2],[2,2,1]] for which OJ returns wrong result.
Definition for singly-linked list.
class ListNode(object): def __init__(self, x): self.val = x self.next = None def __le__(self,other): return isinstance(other, ListNode) and self.val < other.val def __eq__(self,other): return isinstance(other, ListNode) and self.val == other.val def __str__(self): return str(self.val) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.val) class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ cleanLists= for ll in lists: if ll != None: cleanLists.append(ll) if len(cleanLists) == 0: return None import heapq heapq.heapify(cleanLists) heapTop = heapq.heappop(cleanLists) head = heapTop retHead = head if heapTop.next != None: heapq.heappush(cleanLists, heapTop.next) while (len(cleanLists) > 0): heapTop = heapq.heappop(cleanLists) head.next = heapTop if heapTop.next != None: heapq.heappush(cleanLists, heapTop.next) head = head.next return retHead