-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers.py
41 lines (33 loc) · 1.07 KB
/
add-two-numbers.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
39
40
41
# https://leetcode.com/problems/add-two-numbers/
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def convertListToInt(self, head: ListNode) -> int:
result = 0
k = 1
node = head
while node:
result += node.val * k
node = node.next
k *= 10
return result
def convertIntToList(self, number: int) -> ListNode:
if number < 10:
return ListNode(number)
result = tail = ListNode(number % 10)
number //= 10
while number:
digit = number % 10
number //= 10
tail.next = ListNode(digit)
tail = tail.next
return result
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
if not l1 and not l2:
return None
n1 = self.convertListToInt(l1) if l1 else 0
n2 = self.convertListToInt(l2) if l2 else 0
return self.convertIntToList(n1 + n2)