Python solution with linear time complexity

def addTwoNumbers(self, A, B):
current=A
current1=B
llist=[]
temp=0
Val=0
Out=A
while 1:
if current is None and current1 is None:
break
if(current is None):
temp=temp+current1.val
current1=current1.next
elif(current1 is None):
temp=temp+current.val
current=current.next
else:
temp=temp+current.val+current1.val
if(temp<=9):
llist.append(temp)
temp=0
else:
Val=temp%10
llist.append(Val)
temp=1
if(current is not None and current1 is not None):
current=current.next
current1=current1.next
if(temp==1):
llist.append(temp)
for i in range(0,len(llist)):
Out.val=llist[i]
if(Out.next is None and i==(len(llist)-1)):
break
elif(Out.next is None):
Out.next=ListNode(0);
Out=Out.next
return A

Click here to start solving coding interview questions