-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00002-AddTwoNumbers.py
38 lines (32 loc) · 1018 Bytes
/
00002-AddTwoNumbers.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
32
33
34
35
36
37
38
# https://leetcode.com/problems/add-two-numbers/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
"""
Logic: Mathematical Approach with Carry Flag
Time: O(max(m,n))
Space: O(max(m,n))
"""
dummy = ListNode()
head = dummy
carry = 0
while l1 or l2:
a = l1.val if l1 else 0
b = l2.val if l2 else 0
total = a + b + carry
if total >= 10:
carry = 1
total -= 10
else:
carry = 0
head.next = ListNode(total)
head = head.next
if l1: l1 = l1.next
if l2: l2 = l2.next
if carry:
head.next = ListNode(carry)
return dummy.next