-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-two-numbers.py
More file actions
32 lines (31 loc) · 926 Bytes
/
add-two-numbers.py
File metadata and controls
32 lines (31 loc) · 926 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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
# Time: O(n), n is the length of longer linked-list
# Space: O(n), n is the length of result
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy = ListNode(0)
p = dummy
carry = 0
while l1 is not None or l2 is not None:
x = l1.val if l1 is not None else 0
y = l2.val if l2 is not None else 0
s = x + y + carry
carry = s // 10
p.next = ListNode(s % 10)
p = p.next
if l1 is not None:
l1 = l1.next
if l2 is not None:
l2 = l2.next
if carry != 0:
p.next = ListNode(carry)
return dummy.next