def main(): # Build the linked lists with the test inputs # Start pointer references the head, which we pass to solution listOneStart = listOne = ListNode(2) listOne.next = ListNode(4) listOne = listOne.next listOne.next = ListNode(3) listOne = listOneStart listTwoStart = listTwo = ListNode(5) listTwo.next = ListNode(6) listTwo = listTwo.next listTwo.next = ListNode(4) listTwo = listTwoStart # Instantiate Solution class s = Solution() # Run the Solution method answer = s.addTwoNumbers(listOne, listTwo) # Loop through answer list and print all nodes while answer: print(str(answer.val)) answer = answer.next
from list.ListNode import ListNode from Solution import Solution head = [2, 4, 3] head2 = [5, 6, 4] s = Solution() print( ListNode.listNodeToString( s.addTwoNumbers(ListNode.stringToListNode(head), ListNode.stringToListNode(head2))))
from Solution import Solution # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None # 这个使用from Solution import Solution引入 # main方法 if __name__ == '__main__': slt = Solution() listnode1 = ListNode(2) listnode1.next = ListNode(4) listnode1.next.next = ListNode(3) listnode2 = ListNode(5) listnode2.next = ListNode(6) listnode2.next.next = ListNode(4) temp = slt.addTwoNumbers(listnode1, listnode2) print(temp.val, temp.next.val, temp.next.next.val) # 7 0 8
from Solution import Solution s = Solution() print s.addTwoNumbers(l1=[2,4],l2=[5,6,4])
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() l1 = ListNode(2, ListNode(4, ListNode(3))) l2 = ListNode(5, ListNode(6, ListNode(4))) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)), second=str(ListNode(7, ListNode(0, ListNode(8)))), msg='Not Equal => Except value and Actual value') l1 = ListNode(0) l2 = ListNode(0) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)), second=str(ListNode(0)), msg='Not Equal => Except value and Actual value') l1 = ListNode( 9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9))))))) l2 = ListNode(9, ListNode(9, ListNode(9, ListNode(9)))) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)),
from Solution import ListNode from Solution import Solution l1 = ListNode(2) t1 = l1 t1.next = ListNode(4) t1 = t1.next t1.next = ListNode(3) l2 = ListNode(5) t2 = l2 t2.next = ListNode(6) t2 = t2.next t2.next = ListNode(4) sum = Solution() print(sum.addTwoNumbers(l1, l2))