divmod(a,b)函数
中文说明:
divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数
返回结果类型为tuple
参数:
a,b可以为数字(包括复数)
from
1 # Definition for singly-linked list. 2 # class ListNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution(object): 8 def addTwoNumbers(self, l1, l2): 9 """ 10 :type l1: ListNode 11 :type l2: ListNode 12 :rtype: ListNode 13 """ 14 root=res=ListNode(1) 15 extra=0 16 while l1 or l2 or extra: 17 one=two=0 18 if l1: 19 one=l1.val 20 l1=l1.next 21 if l2: 22 two=l2.val 23 l2=l2.next 24 #divmod内置函数 25 extra,temp=divmod(one+two+extra,10) 26 res.next=ListNode(temp) 27 res=res.next 28 return root.next 29