-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path445.py
47 lines (41 loc) · 1022 Bytes
/
445.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
#!/usr/bin/env python
# coding=utf-8
# Definition for singly-linked list.
# 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
"""
num1, num2 = 0, 0
a1, a2, a3= [], [], []
while l1:
a1.append(l1.val)
l1 = l1.next
while l2:
a2.append(l2.val)
l2 = l2.next
p = 1
while a1:
num1 += a1.pop()*p
p *= 10
p = 1
while a2:
num2 += a2.pop()*p
p *= 10
num3 = num1 + num2
while num3 > 0:
a3.append(num3%10)
num3 /= 10
p = ListNode(a3.pop())
tmp = p
while a3:
l = ListNode(a3.pop())
tmp.next = l
tmp = tmp.next
return p