-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_002_AddTwoNumbers.py
More file actions
40 lines (34 loc) · 986 Bytes
/
_002_AddTwoNumbers.py
File metadata and controls
40 lines (34 loc) · 986 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
29
30
31
32
33
34
35
36
37
38
39
40
#-----------------------------------------------------------------------------
# Runtime: 112ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
import sys
sys.path.append('LeetCode')
from ListNode import ListNode
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
result = ListNode(-1)
current = result
carry = 0
while l1 or l2 or carry:
sum = 0
if l1 is not None:
sum += l1.val
l1 = l1.next
if l2 is not None:
sum += l2.val
l2 = l2.next
sum += carry
carry = 0
if sum >= 10:
sum -= 10
carry = 1
current.next = ListNode(sum)
current = current.next
return result.next