-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTwoNum.py
53 lines (43 loc) · 1.19 KB
/
addTwoNum.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def adder(self,n1,n2,n3):
a = n1+n2+n3
c = 0
if a >= 10:
a = a-10
c = 1
return (a,c)
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
cur1 = l1
cur2 = l2
a = self.adder(l1.val, l2.val, 0)
prev_ans = ListNode(a[0])
first_ans = prev_ans
if cur1 is not None:
cur1 = cur1.next
if cur2 is not None:
cur2 = cur2.next
while (cur1 is not None or cur2 is not None or a[1]!= 0):
x = 0
y = 0
if cur1 is not None:
x = cur1.val
if cur2 is not None:
y = cur2.val
a = self.adder(x,y,a[1])
nnode = ListNode(a[0])
prev_ans.next = nnode
prev_ans = nnode
if cur1 is not None:
cur1 = cur1.next
if cur2 is not None:
cur2 = cur2.next
return first_ans