def test_sort_list(self): s = Solution() self.assertEqual('1->2->3->4', s.sortList(build_list('4->2->1->3')).entire_list()) self.assertEqual( '-1->0->3->4->5', s.sortList(build_list('-1->5->3->4->0')).entire_list())
def test_remove_elements(self): s = Solution() self.assertEqual( '1->2->3->4->5', s.removeElements(build_list('1->2->6->3->4->5->6'), 6).entire_list()) self.assertEqual(None, s.removeElements(build_list('1->1'), 1))
def test_build_list(self): list_str = "1->2->3" h1 = build_list(list_str) self.assertEqual(list_str, entire_list(h1)) h2 = build_list([1, 2, 3]) self.assertEqual(list_str, entire_list(h2))
def test_rotate_right(self): s = Solution() self.assertEqual( '4->5->1->2->3', s.rotateRight(build_list('1->2->3->4->5'), 2).entire_list()) self.assertEqual('2->0->1', s.rotateRight(build_list('0->1->2'), 4).entire_list()) self.assertEqual('1->2', s.rotateRight(build_list('1->2'), 2).entire_list())
def test_delete_duplicates(self): s = Solution() ListNode() self.assertEqual( '1->2', s.deleteDuplicates(build_list('1->1->2')).entire_list()) self.assertEqual( '1->2->3', s.deleteDuplicates(build_list('1->1->2->3->3')).entire_list())
def test_reorder_list(self): s = Solution() head1 = build_list('1->2->3->4') s.reorderList(head1) self.assertEqual('1->4->2->3', head1.entire_list()) head2 = build_list('1->2->3->4->5') s.reorderList(head2) self.assertEqual('1->5->2->4->3', head2.entire_list())
def test_add_two_numbers(self): s = Solution() l1 = build_list([2, 4, 3]) l2 = build_list([5, 6, 4, 8]) res = s.addTwoNumbers(l1, l2) print(entire_list(res)) l1 = build_list([6, 1]) l2 = build_list([4]) res = s.addTwoNumbers(l1, l2) print(entire_list(res))
def test_merge_klists(self): lists_str = [ "1->4->5", "1->3->4", "2->6", ] lists = [build_list(s) for s in lists_str] s = Solution() res = s.mergeKLists(lists) self.assertEqual("1->1->2->3->4->4->5->6", entire_list(res))
def test_reverse_between(self): s = Solution() self.assertEqual( '1->4->3->2->5', s.reverseBetween(build_list('1->2->3->4->5'), 2, 4).entire_list())
def test_merge_two_lists(self): h1 = build_list("1->2->4") h2 = build_list("1->3->4") s = Solution() res = s.mergeTwoLists(h1, h2) self.assertEqual("1->1->2->3->4->4", entire_list(res))
def test_swap_pairs(self): s = Solution() ListNode() self.assertEqual('2->1->4->3', s.swapPairs(build_list("1->2->3->4")).entire_list())
def test_sorted_list_to_bst(self): s = Solution() res = s.sortedListToBST(build_list([-10, -3, 0, 5, 9])) print(res)
def test_remove_nth_from_end(self): head = build_list([1, 2, 3, 4, 5]) s = Solution() h = s.removeNthFromEnd(head, 2) self.assertEqual(entire_list(h), "1->2->3->5")
def test_delete_node(self): s = Solution() head = build_list([4, 5, 1, 9]) print('before delete', entire_list(head)) h = s.deleteNode(head, 5) print('after delete', entire_list(h))
def test_add_two_numbers(self): s = Solution() self.assertEqual( '7->8->0->7', s.addTwoNumbers(build_list('7->2->4->3'), build_list('5->6->4')).entire_list())
def test_get_kth_from_end(self): head = build_list([1, 2, 3, 4, 5]) s = Solution() res = s.getKthFromEnd(head, 2) self.assertEqual(entire_list(res), "4->5")