Skip to content

Commit 3a2ad5e

Browse files
authored
sum of lists
1 parent 2ec6195 commit 3a2ad5e

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
"""
2+
Cracking the coding interview
3+
page #95, question #2.5
4+
5+
You have two numbers represented by a linked list, where each node contains a single digit.
6+
The digits are stored in reverse order, such that the 1's digit is at the head of the list.
7+
Write a function that adds the two numbers and returns the sum as a linked list.
8+
9+
Example:
10+
7->1->6 + 5->9->2 That is 617+295 = 912
11+
head of list 1 is 7
12+
head of list 2 is 5
13+
14+
Follow up
15+
16+
Suppose the digits are stored in forward order. Repeat the above problem.
17+
6->1->7 + 2->9->5 That is 617+295 = 912
18+
19+
head of list 1 is 6
20+
head of list 2 is 2
21+
22+
"""
23+
class Stack():
24+
def __init__(self) -> None:
25+
self.s = [None]*6
26+
self.top = None
27+
28+
def push(self,data):
29+
if self.top == None :
30+
self.top = 0
31+
self.s[self.top] = data
32+
else:
33+
self.top +=1
34+
self.s[self.top ] =data
35+
36+
def pop(self):
37+
if self.top == -1:
38+
print("stack empty")
39+
self.top = None
40+
else:
41+
t = self.s[self.top]
42+
# self.s.pop(self.top)
43+
del self.s[self.top]
44+
self.top -=1
45+
return t
46+
def show(self):
47+
p = self.top
48+
while p !=None:
49+
print(self.s[p])
50+
p -=1
51+
print("============")
52+
53+
54+
class Node():
55+
def __init__(self,data) -> None:
56+
self.data = data
57+
self.next = None
58+
59+
class singlelinkedlist():
60+
def __init__(self) -> None:
61+
self.head = None
62+
63+
def insert(self,data):
64+
"""
65+
insert the data to the linked list
66+
"""
67+
node = Node(data)
68+
if self.head is None:
69+
self.head = node
70+
else:
71+
p=self.head
72+
while p.next:
73+
p = p.next
74+
p.next =node
75+
76+
def show(self):
77+
print("------------------------")
78+
p =self.head
79+
if p:
80+
while p:
81+
print(p.data)
82+
p = p.next
83+
84+
else:
85+
print("List empty")
86+
print("------------------------")
87+
88+
class addDigits():
89+
def __init__(self,lobj1,lobj2) -> None:
90+
# self.result = super().__init__()
91+
self.d1 = lobj1
92+
self.d2 = lobj2
93+
94+
95+
def addDigitsinreverse(self):
96+
"""
97+
Add the digits stored in reverse order, Meaning head points to unit digit
98+
return other list if any of the list is empty
99+
100+
"""
101+
102+
if self.d1.head == None:
103+
return self.d2
104+
elif self.d2.head == None:
105+
return self.d1
106+
else:
107+
p1 =self.d1.head
108+
p2 =self.d2.head
109+
carry =0
110+
self.result = singlelinkedlist()
111+
while p1 !=None or p2!=None:
112+
if p1 and p2:
113+
r = p1.data + p2.data +carry
114+
p1 = p1.next
115+
p2 = p2.next
116+
elif p1:
117+
r = p1.data+carry
118+
p1 = p1.next
119+
else:
120+
r = p2.data+carry
121+
p2 = p2.next
122+
123+
carry = 0
124+
if r>=10:
125+
self.result.insert(r%10)
126+
carry = r //10
127+
else:
128+
self.result.insert(r)
129+
130+
if carry >0:
131+
self.result.insert(carry)
132+
133+
134+
return self.result
135+
136+
def addDigitsinforward_withstack(self):
137+
"""
138+
Add the digits stored in list in forward order. Meaning head points to last place of digit
139+
140+
"""
141+
if not self.d1.head:
142+
return self.d2
143+
elif not self.d2.head:
144+
return self.d1
145+
else:
146+
147+
p1 = self.d1.head
148+
p2 = self.d2.head
149+
150+
stack = Stack()
151+
result = singlelinkedlist()
152+
153+
s1 = self.d1.head
154+
s2 = self.d2.head
155+
156+
size1 = 0
157+
size2 = 0
158+
159+
while s1:
160+
size1 +=1
161+
s1 =s1.next
162+
163+
while s2:
164+
size2 +=1
165+
s2 =s2.next
166+
167+
if size2>size1:
168+
pass
169+
else:
170+
pass
171+
172+
173+
174+
175+
while p1 or p2:
176+
177+
if not p1:
178+
stack.push( p2.data)
179+
p2 = p2.next
180+
elif not p2:
181+
stack.push( p1.data)
182+
p1 = p1.next
183+
else:
184+
stack.push(p1.data + p2.data)
185+
p1 = p1.next
186+
p2 = p2.next
187+
188+
189+
190+
carry = 0
191+
while stack.top>=0:
192+
add = stack.pop()
193+
194+
node = Node(add%10+carry)
195+
if add >=9:
196+
197+
if result.head == None:
198+
#
199+
result.head = node
200+
201+
202+
else:
203+
node.next = result.head
204+
result.head = node
205+
206+
carry = 1
207+
208+
209+
else:
210+
if result.head == None:
211+
212+
result.head = node
213+
else:
214+
node.next = result.head
215+
result.head = node
216+
carry = 0
217+
218+
return result
219+
220+
221+
222+
223+
224+
225+
226+
# lobj1 = singlelinkedlist()
227+
# lobj1.insert(8)
228+
# lobj1.insert(9)
229+
# lobj1.show()
230+
231+
# lobj2 = singlelinkedlist()
232+
# lobj2.insert(2)
233+
# lobj2.insert(0)
234+
# lobj2.insert(1)
235+
236+
# lobj2.show()
237+
238+
# aobj1 = addDigits(lobj1,lobj2)
239+
# res1 = aobj1.addDigitsinreverse()
240+
# print("Result")
241+
# res1.show()
242+
243+
#example2 forward
244+
lobj3 = singlelinkedlist()
245+
# lobj3.insert(6)
246+
lobj3.insert(1)
247+
lobj3.insert(7)
248+
249+
lobj4 = singlelinkedlist()
250+
lobj4.insert(2)
251+
lobj4.insert(9)
252+
lobj4.insert(5)
253+
254+
aobj2 = addDigits(lobj3,lobj4)
255+
res2 = aobj2.addDigitsinforward_withstack()
256+
print("Result")
257+
res2.show()
258+
259+

0 commit comments

Comments
 (0)