a1 = tool.ListNode(1) a2 = a1.next = tool.ListNode(2) a3 = a2.next = tool.ListNode(3) ''' a4 = a3.next = tool.ListNode(4) a5 = a4.next = tool.ListNode(5) a6 = a5.next = tool.ListNode(6) a7 = a6.next = tool.ListNode(7) a8 = a7.next = tool.ListNode(8) ''' hi = Solution() test_case = [ #(a1, 0), #(a1, 1), #(a1, 2), (a1, 3) #(a3, 2) ]# common for item in test_case: print "\n\nInput:" tool.print_list(item[0]) print item[1] print "Output:" tool.print_list(hi.removeNthFromEnd(item[0], item[1]))
return new_head a1 = tool.ListNode(1) a2 = a1.next = tool.ListNode(2) a3 = a2.next = tool.ListNode(3) a4 = a3.next = tool.ListNode(4) a5 = a4.next = tool.ListNode(5) a6 = a5.next = tool.ListNode(6) a7 = a6.next = tool.ListNode(7) a8 = a7.next = tool.ListNode(8) hi = Solution() test_case = [ #a1, #a7, a8, None ]# common for item in test_case: print "\n\nInput:" tool.print_list(item) print "Output:" tool.print_list(hi.swapPairs(item))
p.next = p2 p2 = p2.next p = p.next if p1: p.next = p1 elif p2: p.next = p2 return head.next a0 = ListNode(1) a1 = a0.next = ListNode(3) a2 = a1.next = ListNode(5) b0 = ListNode(2) b1 = b0.next = ListNode(4) b2 = b1.next = ListNode(6) hi = Solution() test_case = [ (None, None), (a0, None), (a0, b0) ] # common for item in test_case: print "\n\nInput:", item, " \noutput:" tool.print_list(hi.mergeTwoLists(item[0], item[1]))