Example #1
0
from models.linked_list_node import LinkedListNode


def partition_linked_list(current_node: LinkedListNode, partition: int):
    if current_node.next is not None:
        (connect_node, last_node) = partition_linked_list(current_node.next, partition)
    else:
        return (current_node, current_node)
    if current_node.value >= partition and current_node.next is not None:
        last_node.next = current_node
        current_node.next = None
        return (connect_node, current_node)
    else:
        current_node.next = connect_node
    return (current_node, last_node)


if __name__ == '__main__':
    node_count = 20

    first_node = create_linked_list(node_count)
    print_linked_list('Initial list 1:', first_node)
    print()

    partition = int(input('Partition value: '))

    (first_node, _) = partition_linked_list(first_node, partition)
    print_linked_list('After partitioning:', first_node)
    print()
Example #2
0
    return (result, carryover)


def pad_linked_list_from_left(head: LinkedListNode, count: int, value: int):
    node = None
    for i in range(count):
        node = LinkedListNode(head.id - 1, value)
        node.next = head
        head = node
    return node


if __name__ == '__main__':
    first_number = create_linked_list(3)
    print_linked_list('First number reverse:', first_number)
    second_number = create_linked_list(3)
    print_linked_list('Second number reverse:', second_number)
    print()

    sum = linked_list_to_number_reverse_order(first_number) + linked_list_to_number_reverse_order(second_number)
    print(f'Sum (reverse): {sum}')

    print_linked_list('Sum list (reverse):', number_to_linked_list_reverse_order(sum))
    print()

    first_number = create_linked_list(3)
    print_linked_list('First number forward:', first_number)
    second_number = create_linked_list(3)
    print_linked_list('Second number forward:', second_number)
Example #3
0
def remove_duplicates_from_sorted(first_node):
    current_node = first_node
    prev_node = None
    while current_node is not None:
        if prev_node is not None and prev_node.value == current_node.value:
            prev_node.next = current_node.next
        else:
            prev_node = current_node
        current_node = current_node.next


if __name__ == '__main__':
    node_count = 20
    first_node = create_linked_list(node_count)
    print_linked_list('With duplicates unsorted:', first_node)
    print()

    remove_duplicates_from_unsorted2(first_node)
    print_linked_list('Duplicates removed unsorted:', first_node)
    print()

    first_node = create_linked_list(node_count)
    print_linked_list('With duplicates unsorted:', first_node)
    print()

    first_node = sort_linked_list(first_node)
    print_linked_list('With duplicates sorted (not working yet):', first_node)
    print()

    remove_duplicates_from_sorted(first_node)
    while fast_runner is not None and fast_runner.next is not None:
        first_half_stack.append(slow_runner.value)
        slow_runner = slow_runner.next
        fast_runner = fast_runner.next.next
    if fast_runner is not None:
        slow_runner = slow_runner.next
    while slow_runner is not None:
        if first_half_stack.pop() != slow_runner.value:
            return False
        slow_runner = slow_runner.next
    return True


if __name__ == '__main__':
    first_node = create_letter_linked_list('anklna')
    print_linked_list('Word:', first_node)
    print()
    reversed_list = reverse_linked_list(first_node)
    print_linked_list('Reversed:', reversed_list)
    print()

    is_palindrome = check_is_palindrome(first_node)
    print('IS palindrome' if is_palindrome else 'Is NOT palindrome')
    print()

    is_palindrome = check_is_palindrome2(first_node)
    print('IS palindrome indeed'
          if is_palindrome else 'Is indeed NOT palindrome')
    print()

    is_palindrome = check_is_palindrome3(first_node)
def iterate_linked_list3(first_node: LinkedListNode, k: int):
    runner = first_node
    runner_count = 0
    while runner is not None:
        runner_count += 1
        runner = runner.next
        if runner_count > k:
            first_node = first_node.next
    return first_node


if __name__ == '__main__':
    k = int(input('kth: '))
    node_count = 10
    first_node = create_linked_list(node_count)
    print_linked_list('Initial list 1:', first_node)
    print()

    print_linked_list(
        f'{k}th to last element:',
        linked_list_skip2(first_node,
                          count_linked_list(first_node) - k))
    print()

    first_node = create_linked_list(node_count)
    print_linked_list('Initial list 2:', first_node)
    print()

    print_linked_list(f'{k}th to last element, recursive call:',
                      iterate_linked_list2(first_node, 0, k)[1])
    print()