heappush(h, (n.val, n)) d = ListNode(0) cur = d while len(h) != 0: val, n = heappop(h) cur.next = n cur = cur.next n = n.next if n is not None: heappush(h, (n.val, n)) return d.next if __name__ == "__main__": s = Solution() printLinkedList(s.mergeKLists([None, None])) nums = [1, 2, 4, 6, 8, 10] head1 = buildLinkedList(nums) nums = [1, 3, 5, 7, 9] head2 = buildLinkedList(nums) nums = [0] head3 = buildLinkedList(nums) printLinkedList(s.mergeKLists([head1, head2, head3]))
while fast is not None and fast.next is not None: if slow is fast: return True slow = slow.next fast = fast.next.next return False if __name__ == "__main__": s = Solution() nums = [1, 2] head = buildLinkedList(nums) print s.hasCycle(head) nums = [0] head = buildLinkedList(nums) print s.hasCycle(head) nums = [1, 3, 5, 2, 4, 6] head = buildLinkedList(nums) head.next.next.next.next.next.next = head.next.next print s.hasCycle(head) nums = [] head = buildLinkedList(nums) print s.hasCycle(head)