-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.py
More file actions
28 lines (27 loc) · 824 Bytes
/
2.py
File metadata and controls
28 lines (27 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 链表加法
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l3 = cur3 = ListNode(0)
carry = 0 # 进位
while l1 or l2 or carry: # 考虑最后一位产生进位的情况
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1 + v2 + carry, 10) # 将除数和余数结合起来
cur3.next = ListNode(val)
cur3 = cur3.next
return l3.next