Example #1
0
    def test_merge_two_sorted_lists(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(4)
        a.next = b
        b.next = c

        d = ListNode(1)
        e = ListNode(3)
        f = ListNode(4)
        d.next = e
        e.next = f

        self.assertListEqual([1, 1, 2, 3, 4, 4],
                             convert_linked_list_to_list(
                                 solution.mergeTwoLists(a, d)))

        a = ListNode(2)
        b = ListNode(3)
        a.next = b

        self.assertListEqual([1, 2, 3],
                             convert_linked_list_to_list(
                                 solution.mergeTwoLists(a, ListNode(1))))
Example #2
0
    def test_rotate_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual([4, 5, 1, 2, 3],
                             convert_linked_list_to_list(
                                 solution.rotateRight(a, 2)))
        self.assertListEqual([1],
                             convert_linked_list_to_list(
                                 solution.rotateRight(ListNode(1), 0)))
        self.assertIsNone(solution.rotateRight(None, 1))
Example #3
0
    def test_add_two_numbers(self):
        solution = Solution()
        a = ListNode(2)
        b = ListNode(4)
        c = ListNode(3)
        a.next = b
        b.next = c

        d = ListNode(5)
        e = ListNode(6)
        f = ListNode(4)
        d.next = e
        e.next = f

        self.assertListEqual([7, 0, 8],
                             convert_linked_list_to_list(
                                 solution.addTwoNumbers(a, d)))
        self.assertListEqual([0, 1],
                             convert_linked_list_to_list(
                                 solution.addTwoNumbers(
                                     ListNode(5), ListNode(5))))
    def test_flatten_a_multilevel_doubly_linked_list(self):
        solution = Solution()
        n1 = Node(1, None, None, None)
        n2 = Node(2, None, None, None)
        n3 = Node(3, None, None, None)
        n4 = Node(4, None, None, None)
        n5 = Node(5, None, None, None)
        n6 = Node(6, None, None, None)
        n7 = Node(7, None, None, None)
        n8 = Node(8, None, None, None)
        n9 = Node(9, None, None, None)
        n10 = Node(10, None, None, None)
        n11 = Node(11, None, None, None)
        n12 = Node(12, None, None, None)
        n1.next = n2
        n2.prev = n1
        n2.next = n3
        n3.prev = n2
        n3.next = n4
        n4.prev = n3
        n4.next = n5
        n5.prev = n4
        n5.next = n6
        n6.prev = n5
        n3.child = n7
        n7.next = n8
        n8.prev = n7
        n8.next = n9
        n9.prev = n8
        n9.next = n10
        n10.prev = n9
        n8.child = n11
        n11.next = n12
        n12.prev = n11

        self.assertListEqual([],
                             convert_linked_list_to_list(
                                 solution.flatten(None)))
        self.assertListEqual([1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6],
                             convert_linked_list_to_list(solution.flatten(n1)))
    def test_swap_nodes_in_pairs(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        a.next = b
        b.next = c
        c.next = d

        self.assertListEqual([2, 1, 4, 3],
                             convert_linked_list_to_list(
                                 solution.swapPairs(a)))
Example #6
0
    def test_delete_node_in_a_linked_list(self):
        solution = Solution()
        a = ListNode(4)
        b = ListNode(5)
        c = ListNode(2)
        d = ListNode(9)
        a.next = b
        b.next = c
        c.next = d

        solution.deleteNode(b)

        self.assertListEqual(
            [2, 9], convert_linked_list_to_list(b))
Example #7
0
    def test_add_two_numbers(self):
        solution = Solution()
        a = ListNode(7)
        b = ListNode(2)
        c = ListNode(4)
        d = ListNode(3)
        a.next = b
        b.next = c
        c.next = d

        e = ListNode(5)
        f = ListNode(6)
        g = ListNode(4)
        e.next = f
        f.next = g

        self.assertListEqual([7, 8, 0, 7],
                             convert_linked_list_to_list(
                                 solution.addTwoNumbers(a, e)))
        self.assertListEqual([1, 0],
                             convert_linked_list_to_list(
                                 solution.addTwoNumbers(
                                     ListNode(5), ListNode(5))))
Example #8
0
    def test_reverse_linked_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual(
            [1, 4, 3, 2, 5],
            convert_linked_list_to_list(solution.reverseBetween(a, 2, 4)))

        a1 = ListNode(3)
        b1 = ListNode(5)
        a1.next = b1

        self.assertListEqual([5, 3],
                             convert_linked_list_to_list(
                                 solution.reverseBetween(a1, 1, 2)))
    def test_reverse_nodes_in_k_group(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual([2, 1, 4, 3, 5],
                             convert_linked_list_to_list(
                                 solution.reverseKGroup(a, 2)))
Example #10
0
    def test_odd_even_linked_list(self):
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e
        solution = Solution()

        self.assertListEqual([1, 3, 5, 2, 4],
                             convert_linked_list_to_list(
                                 solution.oddEvenList(a)))
    def test_remove_duplicates_from_sorted_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(1)
        c = ListNode(2)
        d = ListNode(3)
        e = ListNode(3)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual([1, 2, 3],
                             convert_linked_list_to_list(
                                 solution.deleteDuplicates(a)))
Example #12
0
    def test_remove_nth_node_from_end_of_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual([1, 2, 3, 5],
                             convert_linked_list_to_list(
                                 solution.removeNthFromEnd(a, 2)))
Example #13
0
    def test_reverse_linked_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(3)
        d = ListNode(4)
        e = ListNode(5)
        a.next = b
        b.next = c
        c.next = d
        d.next = e

        self.assertListEqual(
            [5, 4, 3, 2, 1],
            convert_linked_list_to_list(solution.reverseList(a)))
    def test_merge_k_sorted_lists(self):
        solution = Solution()
        a = ListNode(-1)
        b = ListNode(5)
        c = ListNode(11)
        a.next = b
        b.next = c

        d = ListNode(6)
        e = ListNode(10)
        d.next = e

        self.assertListEqual([-1, 5, 6, 10, 11],
                             convert_linked_list_to_list(
                                 solution.mergeKLists([None, a, None, d])))
        self.assertIsNone(solution.mergeKLists([]))
Example #15
0
    def test_partition_list(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(4)
        c = ListNode(3)
        d = ListNode(2)
        e = ListNode(5)
        f = ListNode(2)
        a.next = b
        b.next = c
        c.next = d
        d.next = e
        e.next = f

        self.assertListEqual([1, 2, 2, 4, 3, 5],
                             convert_linked_list_to_list(
                                 solution.partition(a, 3)))
    def test_copy_list_with_random_pointer(self):
        solution = Solution()
        a = Node(7)
        b = Node(13)
        c = Node(11)
        d = Node(10)
        e = Node(1)
        a.next = b
        a.random = e
        b.next = c
        b.random = a
        c.next = d
        c.random = e
        d.next = e
        e.random = a

        self.assertListEqual([7, 13, 11, 10, 1],
                             convert_linked_list_to_list(
                                 solution.copyRandomList(a)))
    def test_remove_linked_list_elements(self):
        solution = Solution()
        a = ListNode(1)
        b = ListNode(2)
        c = ListNode(6)
        d = ListNode(3)
        e = ListNode(4)
        f = ListNode(5)
        g = ListNode(6)
        a.next = b
        b.next = c
        c.next = d
        d.next = e
        e.next = f
        f.next = g

        self.assertListEqual([1, 2, 3, 4, 5],
                             convert_linked_list_to_list(
                                 solution.removeElements(a, 6)))