Beispiel #1
0
def add_two_numbers(head1: Optional[ListNode],
                    head2: Optional[ListNode]) -> Optional[ListNode]:
    node1 = head1
    node2 = head2
    dummy_node = ListNode(0)
    result = dummy_node
    carry = 0
    while node1 or node2:
        val1 = 0
        val2 = 0
        if node1:
            val1 = node1.val
            node1 = node1.next
        if node2:
            val2 = node2.val
            node2 = node2.next

        total = val1 + val2 + carry
        carry = int(total / 10)
        result.next = ListNode(total % 10)
        result = result.next

    if carry > 0:
        result.next = ListNode(carry)

    return dummy_node.next
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_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]
Beispiel #4
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_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
Beispiel #6
0
 def create_linked_list(values: List[int]) -> Optional[ListNode]:
     """
     Create linked list from values list
     :param values: the values list of linked list
     :type values: list of int
     :return: head node of linked list
     :rtype: ListNode or None
     """
     if not values:
         return None
     head = ListNode(values[0])
     cur_node = head
     for val in values[1:]:
         cur_node.next = ListNode(val)
         cur_node = cur_node.next
     return head
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_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]
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_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]
Beispiel #11
0
def remove_elements(head: Optional[ListNode], val: int) -> Optional[ListNode]:
    dummy_node: ListNode = ListNode(0)
    dummy_node.next = head

    previous: ListNode = dummy_node
    current: Optional[ListNode] = head

    while current:
        if current.val == val:
            previous.next = current.next
            current = current.next
        else:
            previous = current
            current = current.next

    return dummy_node.next
def remove_nth_from_end(head: ListNode, nth: int) -> Optional[ListNode]:
    dummy: ListNode = ListNode(0)
    dummy.next = head
    previous: ListNode = dummy
    lead: ListNode = dummy

    # Move lead forward
    for _ in range(nth + 1):
        lead = lead.next

    # Move through list until lead is None
    while lead:
        previous = previous.next
        lead = lead.next

    # Delete Node by relinking nodes or reassigning head
    previous.next = previous.next.next
    return dummy.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
def merge_two_lists(head1: Optional[ListNode], head2: Optional[ListNode]) -> Optional[ListNode]:
    if not head1 and not head2:
        return None

    node1 = head1
    node2 = head2
    dummy_node = ListNode(0)
    current_node = dummy_node

    while node1 and node2:
        if node1.val <= node2.val:
            current_node.next = node1
            node1 = node1.next
        else:
            current_node.next = node2
            node2 = node2.next
        current_node = current_node.next

    if node1:
        current_node.next = node1
    if node2:
        current_node.next = node2

    return dummy_node.next
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_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
Beispiel #17
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 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_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_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 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]