-
Notifications
You must be signed in to change notification settings - Fork 0
/
002AddTwoNumbers.py
executable file
·51 lines (40 loc) · 1.14 KB
/
002AddTwoNumbers.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
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
"""
m = self.getValueFromList(l1)
if m < 0:
return False
n = self.getValueFromList(l2)
if n < 0:
return False
rlist = self.getListFromValue(m + n)
return rlist
def getListFromValue(self, val):
m_initnode = ListNode(0)
m_node = m_initnode
while val/10 != 0:
m_node.val = val % 10
val = val / 10
m_newnode = ListNode(0)
m_node.next = m_newnode
m_node = m_newnode
m_node.val = val % 10
m_node.next = None
return m_initnode
def getValueFromList(self, node):
nRet, count = 0, 0
nBase = 10
while node.next is not None:
nRet += node.val * (nBase ** count)
count = count + 1
node = node.next
nRet += node.val * (nBase ** count)
return nRet