-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-AddTwoNumbers.py
37 lines (31 loc) · 1.04 KB
/
2-AddTwoNumbers.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
# 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]:
stack1 = []
stack2 = []
while(l1.next):
stack1.append(l1.val)
l1 = l1.next
stack1.append(l1.val)
while(l2.next):
stack2.append(l2.val)
l2 = l2.next
stack2.append(l2.val)
stack1Str = ""
while(len(stack1) > 0): stack1Str = stack1Str + str(stack1.pop())
stack2Str = ""
while(len(stack2) > 0): stack2Str = stack2Str + str(stack2.pop())
sumStr = str(int(stack1Str) + int(stack2Str))
sumStr = sumStr[::-1]
print(sumStr)
node = ListNode(int(sumStr[0]))
firstNode = node
for d in sumStr[1::]:
newNode = ListNode(int(d))
node.next = newNode
node = newNode
return firstNode