Python code doesn't pass OJ but passed on my computer...


  • 0
    F

    Below is my code. Anybody has any ideas why? Thanks.

    When the input is [[0], [1]], the output on OJ is [1, 0]. But on my computer, it is [0, 1], which is correct...

    from heapq import heappush, heappop, heapify
    
    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
        def __lt__(self, other):
            if isinstance(other, self.__class__):
                return self.val < other.val
            return NotImplemented
        def __eq__(self, other):
            if isinstance(other, self.__class__):
                return self.val == other.val
            return NotImplemented
    class Solution(object):
        def mergeKLists(self, lists):
            """
            :type lists: List[ListNode]
            :rtype: ListNode
            """
            K = len(lists)
            h = [lists[i] for i in range(K) if lists[i]]
            heapify(h)
            head, tail = None, None
            while len(h):
                node = heappop(h)
                if head:
                    tail.next = node
                else:
                    head = node
                tail = node
                if node.next:
                    heappush(h, node.next)
            return head
    

Log in to reply
 

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