Ejemplo n.º 1
0
def insert(head: Optional[SinglyLinkedListNode],
           value: int) -> Optional[SinglyLinkedListNode]:
    if not head:
        cyclic_list = SinglyLinkedListNode(value)
        cyclic_list.next = cyclic_list
        return cyclic_list

    previous = head
    node = head.next

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

        previous = node
        node = node.next

        if previous == head:
            break

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

    return head
Ejemplo n.º 2
0
def test_reverse_linked_list():
    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    assert get_list_values(reverse_linked_list_recursive(head)) == [3, 2, 1]

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

    head = SinglyLinkedListNode(1)
    assert get_list_values(reverse_linked_list_recursive(head)) == [1]
Ejemplo n.º 3
0
def test_insert():
    head = None
    assert get_cyclic_list_values(insert(head, 1)) == [1]

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

    head = SinglyLinkedListNode(3)
    head.next = SinglyLinkedListNode(4)
    head.next.next = SinglyLinkedListNode(1)
    head.next.next.next = head
    assert get_cyclic_list_values(insert(head, 2)) == [3, 4, 1, 2]
def test_detect_cycle_with_set():
    assert detect_cycle_with_set(SinglyLinkedListNode(1)) is None

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

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    head.next.next.next = SinglyLinkedListNode(4)
    head.next.next.next.next = head.next
    assert detect_cycle_with_set(head) == head.next
Ejemplo n.º 5
0
def test_merge_two_lists():

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

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

    list1.next = SinglyLinkedListNode(2)
    list1.next.next = SinglyLinkedListNode(4)
    list2.next = SinglyLinkedListNode(3)
    list2.next.next = SinglyLinkedListNode(4)
    assert get_list_values(merge_two_lists(list1, list2)) == [1, 1, 2, 3, 4, 4]
Ejemplo n.º 6
0
    def append(self, node: SinglyLinkedListNode):
        previous_node = self.head

        if not previous_node:
            self.head = node
            node.next = self.head
            return

        while previous_node.next != self.head:
            previous_node = previous_node.next

        next_node = previous_node.next
        previous_node.next = node
        node.next = next_node
Ejemplo n.º 7
0
def test_remove_nth_from_end():
    head = SinglyLinkedListNode(1)
    assert remove_nth_from_end(head, 1) is None

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    assert remove_nth_from_end(head, 2).data == 2
    assert remove_nth_from_end(head, 1).data == 1

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    head.next.next.next = SinglyLinkedListNode(4)
    assert remove_nth_from_end(head, 2).next.next.data == 4
Ejemplo n.º 8
0
def test_has_cycle():
    assert not has_cycle(None)
    assert not has_cycle(SinglyLinkedListNode(1))

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    head.next.next.next = SinglyLinkedListNode(4)
    head.next.next.next.next = head.next
    assert has_cycle(head)

    head2 = SinglyLinkedListNode(1)
    head2.next = SinglyLinkedListNode(2)
    head2.next.next = SinglyLinkedListNode(3)
    assert not has_cycle(head2)
Ejemplo n.º 9
0
def test_add_two_numbers():
    head1 = SinglyLinkedListNode(1)
    head1.next = SinglyLinkedListNode(2)
    head1.next.next = SinglyLinkedListNode(3)

    head2 = None

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

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

    head1.next = SinglyLinkedListNode(2)
    head2.next = SinglyLinkedListNode(3)
    head2.next.next = SinglyLinkedListNode(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 = SinglyLinkedListNode(1)
    assert get_list_values(rotate_list(head, 1)) == [1]
    assert get_list_values(rotate_list(head, 3)) == [1]

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

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    head.next.next.next = SinglyLinkedListNode(4)
    head.next.next.next.next = SinglyLinkedListNode(5)
    assert get_list_values(rotate_list(head, 2)) == [4, 5, 1, 2, 3]
Ejemplo n.º 11
0
def test_odd_even_linked_list():
    head = SinglyLinkedListNode(1)
    assert odd_even_list(head) == head

    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(3)
    head.next.next.next = SinglyLinkedListNode(4)
    head.next.next.next.next = SinglyLinkedListNode(5)
    assert get_list_values(odd_even_list(head)) == [1, 3, 5, 2, 4]
def reverse_linked_list_recursive(
        head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
    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
Ejemplo n.º 13
0
def test_is_palindrome():
    head = SinglyLinkedListNode(1)
    assert is_palindrome(head)

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

    head.next.next = SinglyLinkedListNode(1)
    assert is_palindrome(head)
def test_get_intersection_node():
    head1 = SinglyLinkedListNode(1)
    head1.next = SinglyLinkedListNode(2)

    head2 = SinglyLinkedListNode(1)
    head2.next = SinglyLinkedListNode(2)
    head2.next.next = SinglyLinkedListNode(3)
    assert get_intersection_node(head1, head2) is None

    head1 = SinglyLinkedListNode(1)
    head2 = head1
    assert get_intersection_node(head1, head2) is head1

    head1 = SinglyLinkedListNode(1)
    head1.next = SinglyLinkedListNode(2)
    head1.next.next = SinglyLinkedListNode(3)
    head1.next.next.next = SinglyLinkedListNode(4)
    head2 = SinglyLinkedListNode(1)
    head2.next = head1.next.next
    assert get_intersection_node(head1, head2) is head1.next.next
Ejemplo n.º 15
0
def test_remove_elements():
    assert remove_elements(None, 1) is None

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

    head = SinglyLinkedListNode(1)
    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(2)
    head.next.next.next = SinglyLinkedListNode(3)
    assert get_list_values(remove_elements(head, 2)) == [1, 3]
Ejemplo n.º 16
0
def test_find_middle_node():
    head = SinglyLinkedListNode(1)
    assert find_middle_node(head) == head

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

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

    head.next.next.next = SinglyLinkedListNode(4)
    assert find_middle_node(head) == head.next
def test_delete_duplicates():
    head = None
    assert delete_duplicates(head) is None

    head = SinglyLinkedListNode(1)
    assert get_list_values(delete_duplicates(head)) == [1]

    head.next = SinglyLinkedListNode(2)
    head.next.next = SinglyLinkedListNode(2)
    head.next.next.next = SinglyLinkedListNode(3)
    head.next.next.next.next = SinglyLinkedListNode(4)
    head.next.next.next.next.next = SinglyLinkedListNode(4)
    assert get_list_values(delete_duplicates(head)) == [1, 2, 3, 4]
Ejemplo n.º 18
0
 def insert(self, data: T, index: int) -> None:
     node = SinglyLinkedListNode(data)
     current_node = self.__head
     count = 0
     while current_node:
         if count == index:
             next_node = current_node.next
             node.next = next_node
             current_node.next = node
             return
         current_node = current_node.next
         count += 1
     if count < index:
         IndexError('Index out of bounds')
Ejemplo n.º 19
0
 def head(self, node: SinglyLinkedListNode):
     self.__head = node
     if node:
         node.next = self.__head