Exemplo n.º 1
0

def no_buffer_solution(linked_list):
    current = linked_list

    while current:

        runner = current
        while runner.next:
            if runner.next.val == current.val:
                runner.next = runner.next.next
            else:
                runner = runner.next

        current = current.next

    return linked_list


if __name__ == "__main__":
    linked_list = Node("head")

    node = linked_list
    for x in [1, 2, 3, 4, 5, 2, 3, 1, 7]:
        new_node = Node(x)
        node.next = new_node
        node = new_node

    print(f"Solution({linked_list})", buffer_solution(linked_list))
    print(f"Solution({linked_list})", no_buffer_solution(linked_list))
Exemplo n.º 2
0
 def new(cls, node: Node):
     if node.as_tuple() not in cls.cache:
         cls.cache[node.as_tuple()] = CalcNode(node)
     return cls.cache[node.as_tuple()]
Exemplo n.º 3
0

def _get_kth_node(node, kth):
    while kth > 0 and node:
        node = node.next
        kth -= 1
    return node


def insert_after(head, value, node):
    while head:
        if head.val == value:
            head.next = node
        head = head.next


if __name__ == "__main__":
    first = create_list([0, 1, 2, 3, 4, 5, 6, 7])
    second = create_list([11, 12, 13, 14])

    intersecting_node = Node(-1)
    intersecting_node.next = Node(-2)
    intersecting_node.next.next = Node(-3)
    insert_after(first, 7, intersecting_node)
    insert_after(second, 14, intersecting_node)

    print(f"Solution({first}, {second}) -> ",
          brute_force_solution(first, second))
    print(f"Solution({first}, {second}) -> ",
          count_and_compare_solution(first, second))