-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0002_Add_Two_Numbers.py
38 lines (34 loc) · 1 KB
/
_0002_Add_Two_Numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
# Approach 2 - iteration ##
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1 + v2 + carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
# Approach 1 - recursion
# if not l1: return l2
# if not l2: return l1
# carry, val = divmod(l1.val+l2.val, 10)
# node = ListNode(val)
# if carry: l1.next = self.addTwoNumbers(l1.next, ListNode(carry))
# node.next = self.addTwoNumbers(l1.next, l2.next)
# return node