-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd Two Numbers.py
59 lines (41 loc) · 1.68 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Link: https://leetcode.com/problems/add-two-numbers/submissions/
class Solution:
def reverseArray(self, arr):
newArr = list()
for elem in arr[::-1]:
newArr.append(elem)
return newArr
def iterateNode(self, headNode):
newArr = list()
while headNode != None:
newArr.append(headNode.val)
headNode = headNode.next
return newArr
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
# Iterate nodes and insert value to array
list1 = self.iterateNode(l1)
list2 = self.iterateNode(l2)
# Reverse both arrays
list1 = self.reverseArray(list1)
list2 = self.reverseArray(list2)
# Turn int to str
s1 = ''.join(str(i) for i in list1)
s2 = ''.join(str(i) for i in list2)
# Add as integer and turn sum into a list
ans = list(str(int(s1) + int(s2)))
# Create a ListNode from 1st element in ans[]
headNode = ListNode(ans[-1])
# Create a new ListNode per element inside ans[]
oldNode = headNode
for i in ans[-2::-1]:
# Create a new ListNode with value i
newNode = ListNode(i)
# Link previous node with new one
oldNode.next = newNode
oldNode = newNode
return headNode