链表_leetcode2

# 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
weight = 1
nums = 0

while cur :
nums += cur.val * weight
weight = weight * 10
cur = cur.next

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

p.next = ListNode(val)
p = p.next
res = res / 10


return head.next


原文地址:https://www.cnblogs.com/lux-ace/p/10557102.html