Beispiel #1
0
def create_level_linked_list(tree, node_lists, level):
    if tree is None:
        return
    node_list = None

    if len(node_lists) == level:
        node_list = LinkedList()
        node_lists.append(node_list)
    else:
        node_list = node_lists[level]

    if node_list.head is None:
        node_list.head = LinkedNode(tree.value)
    else:
        node_list.head.append_to_tail(tree.value)

    create_level_linked_list(tree.left, node_lists, level + 1)
    create_level_linked_list(tree.right, node_lists, level + 1)
Beispiel #2
0
            else:
                before_end.next = n
                before_end = n
        else:
            if after_start is None:
                after_start = n
                after_end = after_start
            else:
                after_end.next = n
                after_end = n
        n = next

    if before_start is None:
        return

    before_end.next = after_start


if __name__ == "__main__":
    linked_list = LinkedList()
    linked_list.head = LinkedNode(3)
    linked_list.head.append_to_tail(5)
    linked_list.head.append_to_tail(8)
    linked_list.head.append_to_tail(5)
    linked_list.head.append_to_tail(10)
    linked_list.head.append_to_tail(2)
    linked_list.head.append_to_tail(1)
    linked_list.list_print()
    partition(linked_list.head, 5)
    linked_list.list_print()
Beispiel #3
0
from Chapter_Two.LinkedList import LinkedList
from Chapter_Two.LinkedNode import LinkedNode


def remove_duplicates(linked_list):
    current = linked_list.head
    while current is not None:
        runner = current
        while runner.next is not None:
            if runner.next.data == current.data:
                runner.next = runner.next.next
            else:
                runner = runner.next
        current = current.next


if __name__ == "__main__":
    linked_list = LinkedList()
    linked_list.head = LinkedNode("t")
    linked_list.head.append_to_tail("r")
    linked_list.head.append_to_tail("e")
    linked_list.head.append_to_tail("e")
    linked_list.head.append_to_tail("t")
    linked_list.head.append_to_tail("a")
    linked_list.list_print()
    remove_duplicates(linked_list)
    linked_list.list_print()
Beispiel #4
0
def list_depth(tree):
    result = list()
    current = None
    if tree is not None:
        current = LinkedNode(tree)
    while current is not None and current.size() > 0:
        result.append(current)
        parents = current
        for i in range(0, parents.size()):
            node = parents
            for j in range(0, i - 1):
                node = node.next
            current = None
            if node.data.left is not None:
                if current is None:
                    current = LinkedNode(node.data.left)
                else:
                    current.next = LinkedNode(node.data.left)
            if node.data.right is not None:
                if current is None:
                    current = LinkedNode(node.data.right)
                else:
                    current.next = LinkedNode(node.data.right)
    return result