看家本领不能丢,准备2021年每天一道leetcode题目,尽量不间隔,Python3.8
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
prenode = ListNode(0)
lastnode = prenode
val = 0
while val or l1 or l2:
val, cur = divmod(val + (l1.val if l1 else 0) + (l2.val if l2 else 0), 10)
lastnode.next = ListNode(cur)
lastnode = lastnode.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return prenode.next
def generateList(l: list) -> ListNode:
prenode = ListNode(0)
lastnode = prenode
for val in l:
lastnode.next = ListNode(val)
lastnode = lastnode.next
return prenode.next
def printList(l: ListNode):
while l:
print("%d, " %(l.val), end = '')
l = l.next
print('')