You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 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
"""
x1 = [l1.val];x2 = [l2.val]
while l1.next!=None:
l1 = l1.next
x1.append(l1.val)
while l2.next!=None:
l2 = l2.next
x2.append(l2.val)
tol1 = 0;tol2 = 0
for i in range(len(x1)):
tol1+=10**i*x1[i]
for i in range(len(x2)):
tol2+=10**i*x2[i]
tol = tol1+tol2
a = [int(i) for i in str(tol)]
a.reverse()
return a