# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode(object):
def __init__(self,x):
self.val=x
self.next=None
class Solution(object):
def addTwoNumbers(self,l1,l2):
num1=""
num2=""
while l1:
num1+=str(l1.val)
l1=l1.next
while l2:
num2+=str(l2.val)
l2=l2.next
add=str(int(num1[::-1])+int(num2[::-1]))[::-1]
head=ListNode(add[0])
ans=head
for i in range(1,len(add)):
node=ListNode(add[i])
head.next=node
head=head.next
return ans
class Solution(object):
def addTwoNumbers(self, l1, l2):
num1 = ‘‘
num2 = ‘‘
answer = head
for i in range(1, len(add)):
node = ListNode(add[i])
head.next = node
head = head.next
return answer
solu=Solution()
l1=ListNode(2)
l2=ListNode(4)
l1.next=l2
l3=ListNode(3)
l2.next=l3
l4=ListNode(5)
l5=ListNode(6)
l4.next=l5
l6=ListNode(4)
l5.next=l6
l=solu.addTwoNumbers(l1,l4)
while l:
print l.val
l=l.next
原文:https://www.cnblogs.com/Marigolci/p/11371790.html