-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Add Two Numbers.py
More file actions
32 lines (27 loc) · 847 Bytes
/
2-Add Two Numbers.py
File metadata and controls
32 lines (27 loc) · 847 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
# Time complexity: O(n + m)
# Space complexity: O(max(n, m))
# n: Length of the first linked list (l1)
# m: Length of the second linked list (l2)
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# Turn linked list to integer
def getInt(self,nl) -> int:
result = 0
power = 0
while nl != None:
result += (nl.val) * pow(10,power)
power += 1
nl = nl.next
return result
# Turn integer to linked list
def getList(self,result) -> ListNode:
ans = None
for i in str(result):
ans = ListNode(int(i),ans)
return ans
def addTwoNumbers(self, l1, l2):
return self.getList(self.getInt(l1) + self.getInt(l2))