def make_link_list(*vals): if not vals: return None start = curr = ListNode(vals[0]) for idx in range(1, len(vals)): curr.next = ListNode(vals[idx]) curr = curr.next return start
def test_2(self): """[1,1], val = 1 returns []""" head = ListNode(1) curr = head curr.next = ListNode(1) curr = obj.removeElements(head, 1) res_list = [] while curr: res_list.append(curr.val) curr = curr.next self.assertEqual(res_list, [])
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])
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
def test_1(self): """[1,2,3,4,5,6], val = 6 returns [1,2,3,4,5]""" head = ListNode(1) curr = head for i in range(2, 7): curr.next = ListNode(i) curr = curr.next obj.removeElements(head, 6) curr = head res_list = [] while curr: res_list.append(curr.val) curr = curr.next self.assertEqual(res_list, [1, 2, 3, 4, 5])
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])
def test_1(self): """[[3,5,7], [0,6], [0,6,28]] returns head of [0,0,3,5,6,6,7,28]""" head1 = ListNode(3) curr = head1 for num in [5, 7]: curr.next = ListNode(num) curr = curr.next head2 = ListNode(0) curr = head2 for num in [6]: curr.next = ListNode(num) curr = curr.next head3 = ListNode(0) curr = head3 for num in [6, 28]: curr.next = ListNode(num) curr = curr.next res_list_head = obj.mergeKLists([head1, head2, head3]) curr = res_list_head res_list = [] while curr: res_list.append(curr.val) curr = curr.next self.assertEqual(res_list, [0, 0, 3, 5, 6, 6, 7, 28])
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)
def test_2(self): """removeNthFromEnd([a], 1) becomes []""" a_node = ListNode("A") self.assertEqual(obj.removeNthFromEnd(a_node, 1), None)