-
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 (32 loc) · 935 Bytes
/
Copy pathadd_two_numbers.py
File metadata and controls
32 lines (32 loc) · 935 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:
# 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]:
x = []
y = []
while l1 or l2:
if l1 != None:
x.append(str(l1.val))
l1 = l1.next
if l2 != None:
y.append(str(l2.val))
l2 = l2.next
temp_x = "".join(x[:])
temp_y = "".join(y[:])
t_1 = int(temp_x[::-1])
t_2 = int(temp_y[::-1])
data = str(t_1 + t_2)
temp = None
tail = None
for i in data[::-1]:
l = ListNode(i)
if temp == None:
temp = l
tail = temp
else:
tail.next = l
tail = l
return temp