-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd2Numbers.py
More file actions
161 lines (130 loc) · 3.94 KB
/
Add2Numbers.py
File metadata and controls
161 lines (130 loc) · 3.94 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes
# contains a single digit. Add the two numbers and return the sum as a linked list.
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 20 20:00:36 2021
@author: Utpal
Version 1
"""
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Link_To_List(object):
def __init__(self, lst, head):
self.lst = lst
self.head = head
def convert(self):
for item in self.lst:
if (self.head == None):
self.head = ListNode(item)
node = self.head
node.val = self.head.val
node.next = None
else:
new_node = ListNode(item)
node.next = new_node
node = node.next
return self.head
class Solution(object):
# def __init__(self, l1, l2):
# self.l1 = l1
# self.l2 = l2
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
#check for either or both lists for null list condition:
if (l1 == None and l2 == None):
return ValueError("Error. Both lists are null")
elif (l1 == None):
return rev_list(lst_to_str(l2))
elif (l2 == None):
return rev_list(lst_to_str(l1))
else:
num1 = lst_to_str(l1)
num2 = lst_to_str(l2)
return rev_list(num1 + num2)
def lst_to_str(lnkdLst):
test_str = ""
while (lnkdLst != None):
test_str = test_str + str(lnkdLst.val)
lnkdLst = lnkdLst.next
return int(test_str[::-1])
def rev_list(rslt):
su = str(rslt)
su = su[::-1]
head = None
for c in su:
if (head == None):
head = ListNode(c)
node = head
node.val = head.val
node.next = None
else:
new_node = ListNode(c)
node.next = new_node
node = node.next
return head
l1 = [2,4,3]
l2 = [5,6,4]
head = None
init_Link_To_List = Link_To_List(l1, head)
lnkd_l1 = init_Link_To_List.convert() #to LL
init_Link_To_List = Link_To_List(l2, head)
lnkd_l2 = init_Link_To_List.convert() #to LL
# sol_obj = Solution(lnkd_l1, lnkd_l2)
su = Solution().addTwoNumbers(lnkd_l1, lnkd_l2)
print (su.val)
#
# Version 2 - uses recursion to reverse
#
class ListNode(object):
def __init__(self, val=0, next=None):
self.data = val
self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: list
"""
l1_lst = self.makeList(l1)
l2_lst = self.makeList(l2)
temp = l1_lst
num1 = Solution().revs(temp, "")
temp = l2_lst
num2 = Solution().revs(temp, "")
rslt = int(num1) + int(num2)
rslt_lst = [int(c) for c in str(rslt)]
rslt_lst_rev = rslt_lst[::-1]
return rslt_lst_rev
def revs(self, head, strn):
if (head == None):
return strn
else:
strn = str(head.data) + strn
return self.revs(head.next, strn)
def makeList(self, lst):
head = None
for elem in lst:
if head is None:
head = ListNode(elem)
curr = head
else:
curr.next = ListNode(elem)
curr = curr.next
# curr.next = None
return head
# l1 = [9,9,9,9,9,9,9]
# l2 = [9,9,9,9]
# l1 = [2,4,3]
# l2 = [5,6,4]
l1 = [0]
l2 = [0]
rslt_lst_rev = Solution().addTwoNumbers(l1, l2)
print (rslt_lst_rev)