Ejemplo n.º 1
0
 def test_addTwoNumbers(self):
     solution = Solution()
     l1 = ListNode(2)
     l1.next = ListNode(4)
     l1.next.next = ListNode(3)
     l2 = ListNode(5)
     l2.next = ListNode(6)
     l2.next.next = ListNode(4)
     lsum = ListNode(7)
     lsum.next = ListNode(0)
     lsum.next.next = ListNode(8)
     solution.addTwoNumbers(l1, l2) == lsum
Ejemplo n.º 2
0
    def test_1(self):
        """[1,3], [2,4,6] returns head of [1,2,3,4,6]"""
        one_node = ListNode(1)
        three_node = ListNode(3)
        one_node.next = three_node

        two_node = ListNode(2)
        four_node = ListNode(4)
        six_node = ListNode(6)
        two_node.next = four_node
        four_node.next = six_node

        res_linked_list = obj.mergeTwoLists(one_node, two_node)

        curr = res_linked_list
        res_list = []
        while curr:
            res_list.append(curr.val)
            curr = curr.next

        self.assertEqual(res_list, [1, 2, 3, 4, 6])
Ejemplo n.º 3
0
    def test_1(self):
        """removeNthFromEnd([a, b, c, d, e], 2) becomes [a, b, c, e]"""
        a_node = ListNode("A")
        b_node = ListNode("B")
        c_node = ListNode("C")
        d_node = ListNode("D")
        e_node = ListNode("E")

        a_node.next = b_node
        b_node.next = c_node
        c_node.next = d_node
        d_node.next = e_node

        self.assertEqual(obj.removeNthFromEnd(a_node, 2), a_node)
        self.assertEqual(c_node.next, e_node)

        counter = 0
        curr = a_node
        while curr:
            curr = curr.next
            counter += 1

        self.assertEqual(counter, 4)
Ejemplo n.º 4
0
    def test_2(self):
        """[1,3], [] returns head of [1,3]"""
        one_node = ListNode(1)
        three_node = ListNode(3)
        one_node.next = three_node

        res_linked_list = obj.mergeTwoLists(one_node, None)

        curr = res_linked_list
        res_list = []
        while curr:
            res_list.append(curr.val)
            curr = curr.next

        self.assertEqual(res_list, [1, 3])