-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.py
31 lines (27 loc) · 985 Bytes
/
2.py
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
29
30
31
# Runtime: 108 ms, faster than 95.44% of Python3 online submissions for Add Two Numbers.
# Difficulty: Medium
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
total, carry = [], 0
while l1 and l2:
digit_sum = l1.val + l2.val + carry
total, carry = total + [digit_sum % 10], digit_sum // 10
l1, l2 = l1.next, l2.next
remaining = l1 if l1 else l2
while remaining:
digit_sum = remaining.val + carry
total, carry = total + [digit_sum % 10], digit_sum // 10
remaining = remaining.next
while carry:
total, carry = total + [carry % 10], carry // 10
rlist = ListNode(total[0])
head = rlist
for element in total[1:]:
rlist.next = ListNode(element)
rlist = rlist.next
return head