-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
50 lines (43 loc) · 1.04 KB
/
sol.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.value = x
self.next = None
def printList(l):
item = l
vals = []
while item:
vals.append(item.value)
item = item.next
print(vals)
def makeList(vals):
vals.reverse()
l = None
for item in vals:
newList = ListNode(item)
newList.next = l
l = newList
return l
def getLeadingZeros(v):
addZeros = 4-len(v)
return "0"*addZeros + v
def getNumberFromLinkedList(l):
strNum = ''
while l:
val = getLeadingZeros(str(l.value))
strNum += val
l = l.next
return int(strNum)
def addTwoHugeNumbers(a, b):
aVal = getNumberFromLinkedList(a)
bVal = getNumberFromLinkedList(b)
total = aVal+bVal
print(total)
return
totalStr = str(total)
chunks = [int(totalStr[i:i+4]) for i in range(0, len(totalStr), 4)]
return makeList(chunks)
a = makeList([123,4,5])
b = makeList([100, 100, 100])
res = addTwoHugeNumbers(a,b)
# printList(res)
# print(getLeadingZeros('21'))