445 add two numbers ii

Best Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def (self, l1, l2):
def valueofList (ls):
val = 0
while ls != None:
val = val *10+ls.val
ls = ls.next
return val
strsum = str(valueofList(l1)+valueofList(l2))
root = ListNode(int(strsum[0]))
p1 = root
for i in range(1,len(strsum)):
node = ListNode(strsum[i])
p1.next = node
p1 = node
return root

Time complexity: O(2(m+n))
Space complexity: O(1)

Transfer list to a value, add them and transfer back