-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-two-numbers.py
35 lines (27 loc) · 955 Bytes
/
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
# 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]:
str1 = ''
str2 = ''
dummy = result = ListNode(0)
while True:
str1 = str(l1.val) + str1
if l1.next == None:
break
else:
l1 = l1.next
while True:
str2 = str(l2.val) + str2
if l2.next == None:
break
else:
l2 = l2.next
outcome = str(int(str1) + int(str2))
for i in range(len(outcome)):
result.next = ListNode(int(outcome[len(outcome)-i-1]))
result = result.next
return dummy.next