首页 > 其他 > 详细

链表_leetcode445

时间:2019-03-19 11:16:19      阅读:187      评论:0      收藏:0      [点我收藏+]
# 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
"""

def getNum(head):

cur = head
res = []
nums = 0

while cur :
res.insert(0,cur.val)
cur = cur.next

weight = 1
for i in res:
nums += i * weight
weight = weight * 10
return nums

nums1 = getNum(l1)
nums2 = getNum(l2)

res = nums1 + nums2
head = ListNode(0)
p = head

if res == 0:
return head

while res:
val = res % 10

newNode = ListNode(val)
newNode.next = p.next
p.next = newNode

res = res / 10

return head.next

链表_leetcode445

原文:https://www.cnblogs.com/lux-ace/p/10557269.html

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