-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_lists.py
351 lines (310 loc) · 6.72 KB
/
linked_lists.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from functools import total_ordering
@total_ordering
class Node(object):
data = None
prev_node = None
next_node = None
def __init__(self, data = None):
self.data = data
def __lt__(self, other):
if other:
return self.data < other.data
else:
return False
def __eq__(self, other):
if other:
return self.data == other.data
else:
return self is None
def __gt__(self, other):
if other:
return self.data > other.data
else:
return False
def __le__(self, other):
if other:
return self.data <= other.data
else:
return False
def __ge__(self, other):
if other:
return self.data >= other.data
else:
return False
def __str__(self):
if(self):
return str(self.data)
else:
return None
class LList(object):
head = None
tail = None
def __init__(self):
pass
def append(self, node):
if (self.head):
cur_node = self.head
while cur_node.next_node:
cur_node = cur_node.next_node
cur_node.next_node = node
node.prev_node = cur_node
else:
self.head = node
def prepend(self, node):
if (self.head):
self.head.prev_node = node
node.next_node = self.head
self.head = node
else:
self.head = node
self.head.next_node = None
def to_array(self):
ll = []
c_node = self.head
while c_node:
ll.append(c_node.data)
c_node = c_node.next_node
return ll
def remove_dups(self):
s = self.head
while s:
sp = s
sn = s.next_node
while sn:
if s == sn:
sp.next_node = sn.next_node
sn = sp.next_node
else:
sp = sn
sn = sn.next_node
s = s.next_node
def return_kth_last_itr(self,k):
cur = self.head
lead = None
lead_count = 0
lead = cur
while lead and lead_count < (k-1):
lead = lead.next_node
lead_count += 1
if(lead_count < (k-1)):
return None
while lead:
lead = lead.next_node
if(lead):
cur = cur.next_node
return cur
def return_kth_last_rec(self, k, node, kth_last_node):
print(kth_last_node)
if(node.next_node):
count = 1 + self.return_kth_last_rec(k, node.next_node, kth_last_node)
if(count == k):
kth_last_node.data = node.data
return count
else:
return 1
def pop(self):
if(self.head):
node = self.head
self.head = self.head.next_node
else:
node = None
return node
def partition(self,pivot):
node = self.pop()
lt_head = None
lt_tail = None
ge_head = None
ge_tail = None
while node:
if(node.data < pivot):
if lt_head:
lt_tail.next_node = node
lt_tail = node
else:
lt_head = node
lt_tail = node
lt_tail.next_node = None
elif(node.data >= pivot):
if ge_head:
ge_tail.next_node = node
ge_tail = node
else:
ge_head = node
ge_tail = node
ge_tail.next_node = None
node = self.pop()
lt_tail.next_node = ge_head
self.head = lt_head
def make_loopy(self,p=0):
#loop to the pth element of the list
node = self.head
if (node):
c = 0
tar = None
tail = None
while node:
if(c == p):
tar = node
if(node.next_node == None):
node.next_node = tar
break
node = node.next_node
c += 1
def find_loop_start(self):
fr = sr = self.head
while sr and fr and fr.next_node:
#print(sr, fr)
sr = sr.next_node
fr = fr.next_node.next_node
if(sr is fr):
#print("collision")
#raw_input()
break
if(sr is not fr):
return -1
else:
sr = self.head
#print(sr)
#print(fr)
count = 0
while sr and fr and sr is not fr:
sr = sr.next_node
fr = fr.next_node
count += 1
if sr is not fr:
return -1
else:
#print("----")
#print(sr)
#print(fr)
return count
def is_palindrome(self):
sr = self.head
fr = self.head
t = LList()
count = 0
while sr and fr:
count += 1
node = sr
t.prepend(node)
if(fr.next_node):
sr = sr.next_node
fr = fr.next_node.next_node
else: #even numbers of elements
break
if(fr is sr):
return True
sr = sr.next_node
pt = t.head
while sr and pt:
if(sr.data != pt.data):
return False
sr = sr.next_node
pt = pr.next_node
return True
def get_length_simple_ll(node):
count = 0
while node:
count += 1
node = node.next_node
return count
def pad_zero_simple_ll(node, p):
if node:
for i in xrange(p):
pad = Node(0)
pad.next_node = node.next_node
node.next_node = pad
pad.data = node.data
node.data = 0
def prep_addition(num1, num2):
len1 = get_length_simple_ll(num1)
len2 = get_length_simple_ll(num2)
p = len1 - len2
if p > 0:
pad_zero_simple_ll(num2, p)
elif p < 0:
pad_zero_simple_ll(num1, -p)
def add_ll_numbers_rec_lsd_first(num1, num2, carry):
if num1 == None and num2 == None and carry == 0:
return None
val = carry
if num1:
val += num1.data
next_num1 = num1.next_node
else:
next_num1 = None
if num2:
val += num2.data
next_num2 = num2.next_node
else:
next_num2 = None
carry = 1 if val > 9 else 0
#print(val, next_num1, next_num2, carry)
val = val % 10
new_digit = Node(val)
#raw_input()
part_sum = add_ll_numbers_rec_lsd_first(next_num1, next_num2, carry)
new_digit.next_node = part_sum
part_sum = new_digit
return part_sum
def _add_ll_numbers_rec_lsd_last(num1, num2, carry):
if(num1 == None and num2 == None):
carry[0] = 0
return None
#equal length asssumes the above statement holds... num2 check is redundant above
part_sum = _add_ll_numbers_rec_lsd_last(num1.next_node, num2.next_node, carry)
val = num1.data + num2.data + carry[0]
carry[0] = 1 if val > 9 else 0
val = val % 10
new_digit = Node(val)
new_digit.next_node = part_sum
part_sum = new_digit
return part_sum
def add_ll_numbers_rec_lsd_last(num1, num2):
carry = [0]
prep_addition(num1, num2)
print_simple_ll(num1, False)
print_simple_ll(num2, False)
result = _add_ll_numbers_rec_lsd_last(num1,num2, carry)
if carry[0]:
msd = Node(carry[0])
msd.next_node = result
result = msd
return result
def print_simple_ll(node, LSD_head = True, max_nodes = None):
arr = []
count = False
if max_nodes == None:
count = False
max_nodes = 1
else:
count = True
node_count = 0
while node and node_count < max_nodes:
if(LSD_head):
arr.insert(0, node.data)
else:
arr.append(node.data)
node = node.next_node
node_count += 1 if count else 0
print arr
def main():
import random
num1 = LList()
num2 = LList()
#for x in xrange(1):
# num1.append(Node(random.randrange(0,10)))
#print_simple_ll(num1.head, False)
middle = 0
for x in xrange(middle):
num2.append(Node(x))
for y in xrange(middle, -1, -1):
num2.append(Node(y))
print(num2.to_array())
print(num2.is_palindrome())
#result = add_ll_numbers_rec_lsd_first(num1.head, num2.head, 0)
#print_simple_ll(result)
#result = add_ll_numbers_rec_lsd_last(num1.head, num2.head)
#print_simple_ll(result, False)
if __name__ == '__main__':
main()