-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol_Add_Two_Numbers.py
More file actions
48 lines (38 loc) · 1017 Bytes
/
sol_Add_Two_Numbers.py
File metadata and controls
48 lines (38 loc) · 1017 Bytes
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
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
_sum = 0
l1stk = []
l2stk = []
while l1 or l2:
if l1:
l1stk.append(l1.val)
l1 = l1.next
if l2:
l2stk.append(l2.val)
l2 = l2.next
i = 0
while l1stk or l2stk:
l1v = l1stk.pop(0) if l1stk else 0
l2v = l2stk.pop(0) if l2stk else 0
_sum += (l1v + l2v) * (10 ** i)
i += 1
root = pre = None
while _sum or (root is None):
v = _sum % 10
_sum //= 10
if pre is None:
root = pre = ListNode(v)
else:
cur = ListNode(v)
pre.next = cur
pre = cur
return root