Exemplo n.º 1
0
 def test_merge_two_sorted_lists(self):
     head = merge_k_lists(
         [generate_linked_list([1, 4, 5]).get_head(),
          generate_linked_list([1, 3, 4]).get_head(),
          generate_linked_list([2, 6]).get_head()])
     result = LinkedList(head=head)
     self.assertListEqual([1, 1, 2, 3, 4, 4, 5, 6], result.to_list())
Exemplo n.º 2
0
 def test_partition_list(self):
     linked_list_1 = generate_linked_list([2, 4, 3])
     linked_list_2 = generate_linked_list([5, 6, 4])
     head = add_two_numbers(linked_list_1.get_head(),
                            linked_list_2.get_head())
     linked_list_3 = LinkedList(singly=True, head=head)
     self.assertListEqual([7, 0, 8], linked_list_3.to_list())
    def test_copy_list_with_random_pointer(self):
        node_1 = Node(1)
        node_2 = Node(2)
        node_1.next = node_2
        node_1.random = node_2
        node_2.random = node_2

        head_copied = copy_random_list(node_1)
        linked_list = LinkedList(head=head_copied)

        self.assertListEqual([1, 2], linked_list.to_list())
        self.assertIs(head_copied.next, head_copied.random)
        self.assertIs(head_copied.next, head_copied.next.random)
Exemplo n.º 4
0
 def assert_operation(val_list, x, result_list):
     linked_list = LinkedList(singly=True)
     linked_list.append_val_list(val_list)
     head = partition(linked_list.get_head(), x)
     linked_list.set_head(head)
     self.assertListEqual(result_list, linked_list.to_list())
 def assert_operation(val_list, result_list):
     linked_list = LinkedList(singly=True)
     linked_list.append_val_list(val_list)
     delete_duplicates(linked_list.get_head())
     self.assertListEqual(result_list, linked_list.to_list())
Exemplo n.º 6
0
 def test_merge_two_sorted_lists(self):
     l1 = generate_linked_list([1, 2, 4])
     l2 = generate_linked_list([1, 3, 4])
     head = merge_two_lists(l1.get_head(), l2.get_head())
     l3 = LinkedList(head=head)
     self.assertListEqual([1, 1, 2, 3, 4, 4], l3.to_list())