[LeetCode] 2. Add Two Numbers

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution:
 8     def addTwoNumbers(self, l1: ListNode, l2: ListNode, lso=0) -> ListNode:
 9         val = l1.val + l2.val + lso
10         lso = val // 10
11         r = ListNode(val % 10)
12         
13         if (l1.next != None or l2.next != None or lso != 0):
14             if l1.next == None:
15                 l1.next = ListNode(0)
16             if l2.next == None:
17                 l2.next = ListNode(0)
18             r.next = self.addTwoNumbers(l1.next, l2.next, lso)
19         
20         return r
相關文章
相關標籤/搜索