PYTHON实现
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head=ListNode(0)
p=head
add=0
while l1 and l2 :
the=l1.val+l2.val+add
p.next=ListNode(the%10)
add=the//10
l1=l1.next
p=p.next
l2=l2.next
if l2:
l1=l2
while l1 :
the =l1.val+add
p.next=ListNode(the%10)
add=the//10
l1=l1.next
p=p.next
if add:
p.next=ListNode(add)
return head.next
执行用时120ms
内存消耗14.1mb 消耗了大量内存。。
2019.8.21