Example #1
0
    def test_reverse_list(self):
        head1 = list_to_linked_list([0, 7, 1, 6, 2])
        head2 = list_to_linked_list([5, 4])

        head2.next.next = head1.next.next.next

        node = list_intersection(head1, head2)
        self.assertEqual(node, head2.next.next)
Example #2
0
    def test_1(self):

        number1 = list_to_linked_list([7, 1, 6])
        number2 = list_to_linked_list([5, 9, 2])
        result = add_linked_list(number1, number2)
        self.assertEqual(result, 912)

        number1 = list_to_linked_list([7, 1, 6, 2])
        number2 = list_to_linked_list([5, 9, 2])
        result = add_linked_list(number1, number2)
        self.assertEqual(result, 2912)
Example #3
0
    def test_a1(self):

        head = list_to_linked_list([3, 5, 8, 5, 10, 2, 1])
        result = partition(5, head)
        self.assertEqual(result.toList(), [3, 2, 1, 5, 8, 5, 10])

        head = list_to_linked_list([5, 6, 6])
        result = partition(5, head)
        self.assertEqual(result.toList(), [5, 6, 6])

        head = list_to_linked_list([5, 6, 6])
        result = partition(10, head)
        self.assertEqual(result.toList(), [5, 6, 6])
Example #4
0
    def test_get_kth_node(self):
        head = list_to_linked_list([0, 7, 1])
        result = get_kth_node(head, 0)
        self.assertEqual(result, head)

        head = list_to_linked_list([0, 7, 1])
        result = get_kth_node(head, 1)
        self.assertEqual(result, head.next)

        head = list_to_linked_list([0, 7, 1])
        result = get_kth_node(head, 2)
        self.assertEqual(result, head.next.next)

        head = list_to_linked_list([0, 7, 1])
        result = get_kth_node(head, 3)
        self.assertEqual(result, head.next.next.next)
Example #5
0
    def test_a1(self):

        head = list_to_linked_list([1, 2, 3, 4, 5, 6])

        result = k_last_element(head, 0)
        self.assertEqual(result.data, 6)

        result = k_last_element(head, -2)
        self.assertEqual(result, None)

        result = k_last_element(head, 6)
        self.assertEqual(result, None)

        result = k_last_element(head, 1)
        self.assertEqual(result.data, 5)

        result = k_last_element(head, 2)
        self.assertEqual(result.data, 4)

        result = k_last_element(head, 5)
        self.assertEqual(result.data, 1)
Example #6
0
 def test_list_lnegth(self):
     head = list_to_linked_list([0, 7, 1, 6, 2])
     result = list_length(head)
     self.assertEqual(result, 5)
Example #7
0
    def test_a1(self):

        head = list_to_linked_list([1, 2, 3, 4, 5, 6])

        delete_node(head.next.next)
        self.assertEqual(head.toList(), [1, 2, 4, 5, 6])