Ejemplo n.º 1
0
def merge_k_lists_naive2(self, lists):
    # Time: O(nk*k) where k is the number of lists, with each list having n nodes
    # since we'll be iterating for a total of nk nodes over k lists
    orig_head = cur_head = ListNode('dummy')
    while True:
        cur_min = ListNode(float('inf'))
        for index, node in enumerate(lists):
            if node is None: continue
            if node.val < cur_min.val:
                cur_min = node
                min_index = index
        if cur_min.val == float('inf'):
            return orig_head.next
        else:
            cur_head.next = cur_min
            lists[min_index] = cur_min.next
            cur_head = cur_head.next
    return orig_head.next


if __name__ == '__main__':
    arr_list = [[10, 15, 25], [5, 12, 20], [1, 4, 7, 11, 17, 22]]
    # arr_list = []
    sorted_list = []
    for arr in arr_list:
        sorted_list.append(construct_linked_list_from_array(arr))
    # print_linked_list(merge_k_sorted_lists(sorted_list))
    # print_linked_list(merge_k_sorted_lists_using_heap(sorted_list))
    print_linked_list(merge_k_sorted_lists_optimized(sorted_list))
Ejemplo n.º 2
0
        while head is None:
            print(head.val)
        return orig_less_than_k.next


if __name__ == '__main__':
    test_cases = [
        ([], []),
        ([1, 2, 3, 4], [1, 4, 2, 3]),
        ([1, 2, 3, 4, 5, 6], [1, 6, 2, 5, 3, 4]),
        ([1, 2, 3, 4, 5], [1, 5, 2, 4, 3]),
        ([1, 2, 3], [1, 3, 2]),
        ([1, 2], [1, 2]),
        ([1], [1]),
    ]
    arr = [
        -8, -2, -7, 4, 17, 16, 15, 6, -3, 8, 13, -11, 9, 22, 2, 3, -4, 11, 1,
        -5, -9, 21, 14, 19, 7, 18, 20, 5, -1, 0, 12, -6, 23, -10, -12, 10
    ]
    head = construct_linked_list_from_array(arr)
    res = Solution().list_pivoting(head, 3)
    print 'res is ', res
    # for index, test_case in enumerate(test_cases, 1):
    #     head = construct_linked_list_from_array(test_case[0])
    #     res = Solution().reorder_list(head)
    #     expected_res = construct_linked_list_from_array(test_case[1])
    #     if compare_two_linked_lists(res, expected_res):
    #         print "%d: Passed" % index
    #     else:
    #         print "%d: Failed" % index
Ejemplo n.º 3
0
        root_node.left = left_node
        # Change head pointer of Linked List for parent recursive calls
        self.head = self.head.next
        # print 'new_head ', self.head
        # Relursively construct the right subtree and link it with root
        # The number of nodes in right subtree  is total nodes - nodes in
        # left subtree - 1 (for root) which is n-n/2-1*/
        root_node.right = self.construct_bst(n - n / 2 - 1)
        # print 'root_node right', root_node.right
        return root_node

    def sorted_linkedlist_to_bst(self, head):
        """
        Idea:
            http://articles.leetcode.com/convert-sorted-list-to-balanced-binary
        Given a singly linked list where elements are sorted in ascending
        order, convert it to a height balanced BST.
        """
        n = self.get_ll_len(head)
        self.head = head
        return self.construct_bst(n)

if __name__ == '__main__':
    arr = [0]
    arr = [1, 3]
    # res = Solution().sortedArrayToBST(arr)
    from linkedlistbase import construct_linked_list_from_array
    head = construct_linked_list_from_array([10, 20, 30, 40, 50, 60])
    head = construct_linked_list_from_array([1])
    res = Solution().sorted_linkedlist_to_bst(head)