def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
    if not head:
        cyclic_list = ListNode(value)
        cyclic_list.next = cyclic_list
        return cyclic_list

    previous = head
    node = head.next

    while node:
        if previous.val <= value <= node.val:
            new_node = ListNode(value)
            previous.next = new_node
            new_node.next = node
            return head
        if previous.val > node.val:
            if value >= previous.val or value <= node.val:
                new_node = ListNode(value)
                previous.next = new_node
                new_node.next = node
                return head

        previous = node
        node = node.next

        if previous == head:
            break

    new_node = ListNode(value)
    previous.next = new_node
    new_node.next = node

    return head
def test_reverse_linked_list():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    assert get_list_values(reverse_linked_list_recursive(head)) == [3, 2, 1]

    head = ListNode(1)
    head.next = ListNode(2)
    assert get_list_values(reverse_linked_list_recursive(head)) == [2, 1]

    head = ListNode(1)
    assert get_list_values(reverse_linked_list_recursive(head)) == [1]
def test_insert():
    head = None
    assert get_cyclic_list_values(insert(head, 1)) == [1]

    head = ListNode(1)
    head.next = head
    assert get_cyclic_list_values(insert(head, 2)) == [1, 2]

    head = ListNode(3)
    head.next = ListNode(4)
    head.next.next = ListNode(1)
    head.next.next.next = head
    assert get_cyclic_list_values(insert(head, 2)) == [3, 4, 1, 2]
def test_merge_two_lists():

    list1 = ListNode(1)
    assert get_list_values(merge_two_lists(list1, None)) == [1]

    list2 = ListNode(1)
    assert get_list_values(merge_two_lists(list1, list2)) == [1, 1]

    list1.next = ListNode(2)
    list1.next.next = ListNode(4)
    list2.next = ListNode(3)
    list2.next.next = ListNode(4)
    assert get_list_values(merge_two_lists(list1, list2)) == [1, 1, 2, 3, 4, 4]
def test_detect_cycle_with_set():
    assert detect_cycle_with_set(ListNode(1)) is None

    head = ListNode(1)
    head.next = head
    assert detect_cycle_with_set(head) == head

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = head.next
    assert detect_cycle_with_set(head) == head.next
def test_remove_nth_from_end():
    head = ListNode(1)
    assert remove_nth_from_end(head, 1) is None

    head = ListNode(1)
    head.next = ListNode(2)
    assert remove_nth_from_end(head, 2).val == 2
    assert remove_nth_from_end(head, 1).val == 1

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    assert remove_nth_from_end(head, 2).next.next.val == 4
def test_add_two_numbers():
    head1 = ListNode(1)
    head1.next = ListNode(2)
    head1.next.next = ListNode(3)

    head2 = None

    assert get_list_values(add_two_numbers(head1, head2)) == [1, 2, 3]

    head1 = ListNode(5)
    head2 = ListNode(5)
    assert get_list_values(add_two_numbers(head1, head2)) == [0, 1]

    head1.next = ListNode(2)
    head2.next = ListNode(3)
    head2.next.next = ListNode(4)
    assert get_list_values(add_two_numbers(head1, head2)) == [0, 6, 4]
def test_rotate_list():
    head = None
    assert rotate_list(head, 1) is None

    head = ListNode(1)
    assert get_list_values(rotate_list(head, 1)) == [1]
    assert get_list_values(rotate_list(head, 3)) == [1]

    head.next = ListNode(2)
    assert get_list_values(rotate_list(head, 1)) == [2, 1]

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = ListNode(5)
    assert get_list_values(rotate_list(head, 2)) == [4, 5, 1, 2, 3]
def reverse_linked_list_recursive(head: ListNode) -> Optional[ListNode]:
    if not head or not head.next:
        return head

    new_head = reverse_linked_list_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head
def test_is_palindrome():
    head = ListNode(1)
    assert is_palindrome(head)

    head.next = ListNode(2)
    assert not is_palindrome(head)

    head.next.next = ListNode(1)
    assert is_palindrome(head)
def test_odd_even_linked_list():
    head = ListNode(1)
    assert odd_even_list(head) == head

    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = ListNode(5)
    assert get_list_values(odd_even_list(head)) == [1, 3, 5, 2, 4]
def test_detect_cycle():
    assert detect_cycle(ListNode(1)) is None

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    assert detect_cycle(head) is None

    head = ListNode(1)
    head.next = head
    assert detect_cycle(head) == head

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = head.next
    assert detect_cycle(head) is head.next
Exemple #13
0
def test_get_intersection_node():
    head1 = ListNode(1)
    head1.next = ListNode(2)

    head2 = ListNode(1)
    head2.next = ListNode(2)
    head2.next.next = ListNode(3)
    assert get_intersection_node_with_set(head1, head2) is None

    head1 = ListNode(1)
    head2 = head1
    assert get_intersection_node_with_set(head1, head2) is head1

    head1 = ListNode(1)
    head1.next = ListNode(2)
    head1.next.next = ListNode(3)
    head1.next.next.next = ListNode(4)
    head2 = ListNode(1)
    head2.next = head1.next.next
    assert get_intersection_node_with_set(head1, head2) is head1.next.next
def test_remove_elements():
    assert remove_elements(None, 1) is None

    head = ListNode(1)
    assert remove_elements(head, 1) is None
    assert remove_elements(head, 2) is head

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(2)
    head.next.next.next = ListNode(3)
    assert get_list_values(remove_elements(head, 2)) == [1, 3]
Exemple #15
0
def test_1():
    head = Node(0)
    head.next = Node(1)
    head.next.next = Node(2)
    head.next.next.next = Node(3)
    head.next.next.next.next = Node(4)
    head.next.next.next.next.next = Node(5)
    head.next.next.next.next.next.next = Node(6)
    head.next.next.next.next.next.next.next = Node(7)
    head.next.next.next.next.next.next.next.next = Node(8)
    head.next.next.next.next.next.next.next.next.next = Node(9)
    head.next.next.next.next.next.next.next.next.next.next = head.next.next.next.next
    assert findLoop(head) == head.next.next.next.next
def test_find_middle_node():
    head = ListNode(1)
    assert find_middle_node(head) == head

    head.next = ListNode(2)
    assert find_middle_node(head) == head

    # 1, 2, 3
    head.next.next = ListNode(3)
    assert find_middle_node(head) == head.next

    head.next.next.next = ListNode(4)
    assert find_middle_node(head) == head.next