···
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
"""
jinji=0
rslt=l1.val+l2.val+jinji
jinji=rslt/10
yushu=rslt-jinji10
head=ListNode(yushu)
l3=head
l1=l1.next
l2=l2.next
while l1!=None and l2!=None:
rslt=l1.val+l2.val+jinji
jinji=rslt/10
yushu=rslt-jinji10
l3.next=ListNode(yushu)
l1=l1.next
l2=l2.next
l3=l3.next
while l1!=None:
rslt=l1.val+jinji
jinji=rslt/10
yushu=rslt-jinji10
l3.next=ListNode(yushu)
l3=l3.next
l1=l1.next
while l2!=None:
rslt=l2.val+jinji
jinji=rslt/10
yushu=rslt-jinji10
l3.next=ListNode(yushu)
l3=l3.next
l2=l2.next
#晋级数
if jinji>0:
l3.next=ListNode(jinji)
l3=l3.next
print head
return head
···
网友评论