def test_reverse_linked_list(linked_list):
    """Test that we reverse the list."""
    from reverse_linked_list import reverse_linked_list
    reverse_linked_list(linked_list)
    assert linked_list.head.data == 1
    assert linked_list.head.next_node.data == 2
    assert linked_list.head.next_node.next_node.data == 3
def test_one_in_linked_list(linked_list):
    """Test get one time back with one item in list."""
    from reverse_linked_list import reverse_linked_list
    linked_list.pop()
    linked_list.pop()
    reverse_linked_list(linked_list)
    assert linked_list.head.data == 1
def test_palindrome_method_1():
    list1 = convert_array_to_linked_list(['f', 'g', 'e', 'g', 'f'])
    list2 = reverse_linked_list(convert_array_to_linked_list(['f', 'g', 'e', 'g', 'f']))
    naive_palindrome_check_by_comparing_list_and_reversed_list(list1, list2)

    list3 = convert_array_to_linked_list(['a', 'g', 'e', 'd', 'f'])
    list4 = reverse_linked_list(convert_array_to_linked_list(['a', 'g', 'e', 'd', 'f']))
    naive_palindrome_check_by_comparing_list_and_reversed_list(list3, list4)
def test_empty_linked_list(linked_list):
    """Test exception from empty linked_list."""
    from reverse_linked_list import reverse_linked_list
    linked_list.pop()
    linked_list.pop()
    linked_list.pop()
    with pytest.raises(IndexError):
        reverse_linked_list(linked_list)
def method_1_reverse_linked_list_after_k(head, k):
    temp_head = head
    length = count_no_of_elements(head)
    if k > length:
        print "Wrong input: k value greater than length"
        return
    if not head:
        print "list is empty"
        return
    count = 0
    prev_head = None
    while temp_head and count < k:
        prev_head = temp_head
        temp_head = temp_head.next
        count += 1
    print "\n head.contents:{0}".format(head)
    print "\n prev head contents:{0}".format(prev_head)
    print "\noriginal list: "
    print_linked_list_contents(head)

    reversed_k_head = reverse_linked_list(temp_head)
    print "\n reversed_k_head list:"
    print_linked_list_contents(reversed_k_head)

    prev_head.next = reverse_linked_list
    print "new::"
Esempio n. 6
0
def test_convert_array_to_linked_list():
    print "# convert array to a linked list"
    head1 = convert_array_to_linked_list(['f', 'g', 'e', 'g', 'f'])
    print_linked_list_contents(head1)

    print "\n# reversed linked list"
    # does not work
    # reversed_head = head1
    reversed_head = reverse_linked_list(convert_array_to_linked_list(['f', 'g', 'e', 'g', 'f']))
    print_linked_list_contents(reversed_head)
def test_long_reverse_linked_list(linked_list):
    """Test that we reverse the list."""
    from reverse_linked_list import reverse_linked_list
    linked_list.push(4)
    linked_list.push(5)
    reverse_linked_list(linked_list)
    assert linked_list.head.data == 1
    assert linked_list.head.next_node.data == 2
    assert linked_list.head.next_node.next_node.data == 3
    assert linked_list.head.next_node.next_node.next_node.data == 4
    assert linked_list.head.next_node.next_node.next_node.next_node.data == 5
    assert linked_list.head.next_node.next_node.next_node.next_node.next_node is None
    reverse_linked_list(linked_list)
    assert linked_list.head.data == 5
    assert linked_list.head.next_node.data == 4
    assert linked_list.head.next_node.next_node.data == 3
    assert linked_list.head.next_node.next_node.next_node.data == 2
    assert linked_list.head.next_node.next_node.next_node.next_node.data == 1
    assert linked_list.head.next_node.next_node.next_node.next_node.next_node is None
Esempio n. 8
0
 def test_one_element(self):
     head = ListNode(1)
     node = head
     expected = [1]
     for i in range(2, 10):
         node.next = ListNode(i)
         node = node.next
         expected.insert(0, i)
     self.assertEqual(
         self.to_list(reverse_linked_list.reverse_linked_list(head)),
         expected)
Esempio n. 9
0
        else:
            lista.append(t_reversed_head.contents)
            t_reversed_head = t_reversed_head.next
        count = count + 1
    lista.append(t_head.contents)
    new_head = generate_linked_list_with_values(lista)
    return new_head

if __name__ == "__main__":
    head = generate_linked_list(6)
    print "actual order :"
    print_linked_list_contents(head)
    print "\nexpected output: 1->6->2->5->3->4"
    middle = middle_element_of_linked_list(head)

    rest_reversed = reverse_linked_list(middle)

    print "\nreversed :"
    print_linked_list_contents(rest_reversed)

    new_order = first_last_ordering_with_stack(head, rest_reversed, middle)
    print "\nnew linked list order :"
    print_linked_list_contents(new_order)

    print "\nnew linked list order without stack:"
    new_call = first_last_ordering_without_stack(head, rest_reversed, middle)
    print_linked_list_contents(new_call)


    # Empty case
    print_linked_list_contents(first_last_ordering_with_stack(None, None, None))
Esempio n. 10
0
        print(head.value, end="")
        while head.next != None:
            head = head.next
            print("-->" + str(head.value), end="")
        print("")


def get_head():
    head1 = Node(0)
    head2 = Node(1, Node(2, Node(2, Node(3, Node(4, Node(5))))))
    return head1, head2


if __name__ == '__main__':
    # 移除指定值得节点, 考虑几种情况,1.只有一个节点的链表,2.初始节点的值为target,3.最后节点的值为target
    head1, _ = get_head()
    print_linked_list(remove_target_node(head1, 0))
    head1, _ = get_head()
    print_linked_list(remove_target_node(head1, 1))
    _, head2 = get_head()
    print_linked_list(remove_target_node(head2, 1))
    _, head2 = get_head()
    print_linked_list(remove_target_node(head2, 2))
    _, head2 = get_head()
    print_linked_list(remove_target_node(head2, 5))

    # 翻转链表
    head1, head2 = get_head()
    print_linked_list(reverse_linked_list(head1))
    print_linked_list(reverse_linked_list(head2))
def test_two_in_linked_list(linked_list):
    """Test that it works with two items."""
    from reverse_linked_list import reverse_linked_list
    linked_list.pop()
    reverse_linked_list(linked_list)
    assert linked_list.head.data == 1