-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Add Two Numbers.py
More file actions
30 lines (28 loc) · 898 Bytes
/
2-Add Two Numbers.py
File metadata and controls
30 lines (28 loc) · 898 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
# 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]:
num = str(self.getNum(l1) + self.getNum(l2))
print(num)
rtn = ListNode(val=int(num[-1]))
curr = rtn
for i in range(-2, -(len(num))-1, -1):
curr.next = ListNode(val=int(num[i]))
curr = curr.next
return rtn
def getNum(self, l):
rtn = []
curr = l
rtn.append(curr.val)
while curr.next:
rtn.append(curr.next.val)
curr = curr.next
rtn.reverse()
print(rtn)
num = 0
for i in range(len(rtn)):
num += rtn[i] * 10**(len(rtn)-i-1)
return num