-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbersII2.py
More file actions
74 lines (64 loc) · 1.69 KB
/
AddTwoNumbersII2.py
File metadata and controls
74 lines (64 loc) · 1.69 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
"""
def getLength(head):
l = 0
while head:
l += 1
head = head.next
return l
def addToFront(val, head):
cur = ListNode(val)
cur.next = head
return cur
n1, n2 = getLength(l1), getLength(l2)
result = None
while n1 > 0 and n2 > 0:
val = 0
if n1 >= n2:
val += l1.val
l1 = l1.next
n1 -= 1
if n1 < n2:
val += l2.val
l2 = l2.next
n2 -= 1
result = addToFront(val, result)
cur, result = result, None
carry = 0
while cur:
carry += cur.val
result = addToFront(carry % 10, result)
carry //= 10
cur = cur.next
if carry > 0:
result = addToFront(carry, result)
return result
if __name__ == '__main__':
nums1 = [int(num) for num in input().split()]
nums2 = [int(num) for num in input().split()]
l1 = ListNode(0)
cur = l1
for num in nums1:
cur.next = ListNode(num)
cur = cur.next
l1 = l1.next
l2 = ListNode(0)
cur = l2
for num in nums2:
cur.next = ListNode(num)
cur = cur.next
l2 = l2.next
solution = Solution()
result = solution.addTwoNumbers(l1, l2)
while result:
print(result.val)
result = result.next