예제 #1
0
def sum_lists_forward(list_a, list_b):
    tail_a = list_a.seek_tail()
    tail_b = list_b.seek_tail()

    sum_list = LinkedList.LinkedList_Single()
    carry = 0

    while tail_a is not None or tail_b is not None:
        if tail_a is None:
            temp_sum = tail_b.data + carry
        elif tail_b is None:
            temp_sum = tail_a.data + carry
        else:
            temp_sum = tail_a.data + tail_b.data + carry
        if temp_sum > 9:
            sum_list.add_node_to_head(temp_sum%10)
            carry = 1
        else:
            sum_list.add_node_to_head(temp_sum)
            carry = 0
        list_a.remove_tail()
        list_b.remove_tail()
        tail_a = list_a.seek_tail()
        tail_b = list_b.seek_tail()

    if carry != 0:
        sum_list.add_node_to_head(carry)
    return sum_list
예제 #2
0
def sum_lists_backward(list_a, list_b):
    current_a = list_a.head
    current_b = list_b.head

    # sum = []
    sum = LinkedList.LinkedList_Single()
    carry = 0

    while current_a is not None or current_b is not None:
        if current_a is None:
            temp_sum = current_b.data + carry
        elif current_b is None:
            temp_sum = current_a.data + carry
        else:
            temp_sum = current_a.data + current_b.data + carry

        if temp_sum > 9:
            # sum.append(temp_sum%10)
            sum.add_node_to_list(temp_sum%10)
            carry = 1
        else:
            # sum.append(temp_sum)
            sum.add_node_to_list(temp_sum)
            carry = 0

        if current_a:
            current_a = current_a.next
        if current_b:
            current_b = current_b.next

    if carry != 0:
        # sum.append(carry)
        sum.add_node_to_list(carry)

    return sum
예제 #3
0
def list_of_depths(children, result_list):
    if len(children) is 0:
        return
    same_depth_list = LinkedList.LinkedList_Single()
    for child in children:
        same_depth_list.add_node_to_list(child)
    result_list.append(same_depth_list)
    children_of_children = []
    current_node = same_depth_list.head
    while current_node is not None:
        if current_node.data.left_child is not None:
            children_of_children.append(current_node.data.left_child)
        if current_node.data.right_child is not None:
            children_of_children.append(current_node.data.right_child)
        current_node = current_node.next
    list_of_depths(children_of_children, result_list)
예제 #4
0
        inner_current = current.next
        while inner_current.next is not None:
            if current.next.data == inner_current.next.data:
                inner_current.next = inner_current.next.next
            else:
                inner_current = inner_current.next
        current = current.next


if __name__ == '__main__':
    linked_list = L.LinkedList()
    linked_list.add_array_of_elements_to_list(
        [34, 1, 2, 1, 4, 5, 34, 6, 7, 34])
    linked_list.print_list()
    linked_list.add(23)
    linked_list.print_list()
    linked_list.remove_element_from_list(4)
    linked_list.print_list()
    remove_duplicates_without_buffer(linked_list)
    linked_list.print_list()

    single_linked_list = L.LinkedList_Single()
    single_linked_list.add_array_of_data_to_linked_list([1, 1, 1, 1])
    single_linked_list.print_list()
    single_linked_list.add_array_of_data_to_linked_list([1, 1, 1, 1, 1])
    single_linked_list.print_list()
    # single_linked_list.remove_node(1)
    remove_duplicates_without_buffer(single_linked_list)
    single_linked_list.print_list()
    print(end="\n")
예제 #5
0
from datastructures import LinkedList


def is_looped(list):
    current = list.head
    traversed_nodes = []
    traversed_nodes.append(current)

    while current.next is not None:
        current = current.next
        if current in traversed_nodes:
            return True
        traversed_nodes.append(current)

    return False


if __name__ == '__main__':
    list_a = LinkedList.LinkedList_Single()
    list_a.add_array_of_data_to_linked_list([1, 2, 3, 4, 5, 6, 7])

    list_a.print_list()
    # list_a.seek_tail().next = list_a.head.next.next

    print(is_looped(list_a))