首页 > 其他 > 详细

tmp

时间:2019-08-18 14:09:03      阅读:89      评论:0      收藏:0      [点我收藏+]
# 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

tmp

原文:https://www.cnblogs.com/Marigolci/p/11371790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!