2. Add Two Numbers
2016-09-14 本文已影响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
"""
dummy=ListNode(-1)
current=dummy
carry=0
while l1 or l2:
val=carry
if l1:
val+=l1.val
l1=l1.next
if l2:
val+=l2.val
l2=l2.next
new_node=ListNode(val%10)
current.next=new_node
current=current.next
carry=val/10
if carry!=0:
new_node=ListNode(carry)
current.next=new_node
return dummy.next