-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastAndSlow.py
More file actions
386 lines (273 loc) · 8.59 KB
/
fastAndSlow.py
File metadata and controls
386 lines (273 loc) · 8.59 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from ctypes import pointer
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def has_cycle(head):
# TODO: Write your code here
fast = head
slow = head
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
print("LinkedList has cycle: " + str(has_cycle(head)))
head.next.next.next.next.next.next = head.next.next
print("LinkedList has cycle: " + str(has_cycle(head)))
head.next.next.next.next.next.next = head.next.next.next
print("LinkedList has cycle: " + str(has_cycle(head)))
main()
def find_cycle_length(head):
fast = head
slow = head
newPointer = slow
count = 0
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
newPointer = slow
break
while True:
newPointer = newPointer.next
count += 1
if newPointer == slow:
break
return count
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.next = head.next.next
print("LinkedList cycle length: " + str(find_cycle_length(head)))
head.next.next.next.next.next.next = head.next.next.next
print("LinkedList cycle length: " + str(find_cycle_length(head)))
main()
def find_cycle_start(head):
length = find_cycle_length(head)
p1 = head
p2 = head
for _ in range(length):
p2 = p2.next
while p1 != p2:
p1 = p1.next
p2 = p2.next
return p1
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.next = head.next.next
print("LinkedList cycle start: " + str(find_cycle_start(head).value))
head.next.next.next.next.next.next = head.next.next.next
print("LinkedList cycle start: " + str(find_cycle_start(head).value))
head.next.next.next.next.next.next = head
print("LinkedList cycle start: " + str(find_cycle_start(head).value))
main()
print("HAPPY NUMBER")
def find_happy_number(num):
slow, fast = num, num
while True:
slow = find_square(slow)
fast = find_square(find_square(fast))
if slow == fast:
if slow == 1:
return True
else:
return False
def find_square(num):
sum = 0
while num > 0:
last_digit = num % 10
sum += last_digit * last_digit
num = num // 10
return sum
def main():
print(find_happy_number(23))
print(find_happy_number(12))
main()
print("FIND MIDDLE OF LINKED LIST")
def find_middle_of_linked_list(head):
fast = head
slow = head
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
return slow
def main():
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
print("Middle Node: " + str(find_middle_of_linked_list(head).value))
head.next.next.next.next.next = Node(6)
print("Middle Node: " + str(find_middle_of_linked_list(head).value))
head.next.next.next.next.next.next = Node(7)
print("Middle Node: " + str(find_middle_of_linked_list(head).value))
main()
# Palindrome LinkedList
'''
Given the head of a Singly LinkedList, write a
method to check if the LinkedList is a palindrome or not.
Your algorithm should use constant space and the input
LinkedList should be in the original form once the algorithm is finished.
The algorithm should have O(N) time complexity where 'N' is the number of nodes in the LinkedList.
Example 1:
Input: 2 -> 4 -> 6 -> 4 -> 2 -> null
Output: true
Example 2:
Input: 2 -> 4 -> 6 -> 4 -> 2 -> 2 -> null
Output: false
'''
def is_palindromic_linked_list(head):
middleNode = find_middle_of_linked_list(head)
reversedListHead = reverseLinkedList(middleNode)
right = reversedListHead
left = head
while left != middleNode:
if left.value == right.value:
left = left.next
right = right.next
else:
printLinkedList(reversedListHead)
printLinkedList(head)
reverseLinkedList(reversedListHead)
printLinkedList(head)
return False
printLinkedList(reversedListHead)
printLinkedList(head)
reverseLinkedList(reversedListHead)
printLinkedList(head)
return True
def reverseLinkedList(node):
p0 = None
p1 = node
while p1 != None:
p2 = p1.next
p1.next = p0
p0 = p1
p1 = p2
#return the head
return p0
def printLinkedList(head):
while head:
print(f"{head.value}->",end=" ")
head = head.next
print()
def main():
head = Node(2)
head.next = Node(4)
head.next.next = Node(6)
head.next.next.next = Node(4)
head.next.next.next.next = Node(2)
print("Is palindrome: " + str(is_palindromic_linked_list(head)))
head.next.next.next.next.next = Node(2)
print("Is palindrome: " + str(is_palindromic_linked_list(head)))
main()
# Rearrange a LinkedList (medium)
'''
Given the head of a Singly LinkedList,
write a method to modify the LinkedList
such that the nodes from the second half of
the LinkedList are inserted alternately to the
nodes from the first half in reverse order.
So if the LinkedList has nodes 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null,
your method should return 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null.
Your algorithm should not use any extra space and the
input LinkedList should be modified in-place.
Example 1:
Input: 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> null
Output: 2 -> 12 -> 4 -> 10 -> 6 -> 8 -> null
Example 2:
Input: 2 -> 4 -> 6 -> 8 -> 10 -> null
Output: 2 -> 10 -> 4 -> 8 -> 6 -> null
'''
print("rearrangeLinkedList")
def rearrangeLinkedList(head):
middleNode = find_middle_of_linked_list(head)
oneAfterMid = middleNode.next
middleNode.next = None
reversedList = reverseLinkedList(oneAfterMid)
firstHalf = head
secondHalf = reversedList
while secondHalf != None:
firstHalfNext = firstHalf.next
secondHalfNext = secondHalf.next
firstHalf.next = secondHalf
secondHalf.next = firstHalfNext
firstHalf = firstHalfNext
secondHalf = secondHalfNext
printLinkedList(head)
return head
def main():
head = Node(2)
head.next = Node(4)
head.next.next = Node(6)
head.next.next.next = Node(8)
head.next.next.next.next = Node(10)
head.next.next.next.next.next = Node(12)
rearrangeLinkedList(head)
main()
# Cycle in a Circular Array (hard)
'''
Cycle in a Circular Array (hard)#
We are given an array containing positive and negative numbers. Suppose the array contains a number ‘M’ at a particular index. Now, if ‘M’ is positive we will move forward ‘M’ indices and if ‘M’ is negative move backwards ‘M’ indices. You should assume that the array is circular which means two things:
If, while moving forward, we reach the end of the array, we will jump to the first element to continue the movement.
If, while moving backward, we reach the beginning of the array, we will jump to the last element to continue the movement.
Write a method to determine if the array has a cycle. The cycle should have more than one element and should follow one direction which means the cycle should not contain both forward and backward movements.
Example 1:
Input: [1, 2, -1, 2, 2]
Output: true
Explanation: The array has a cycle among indices: 0 -> 1 -> 3 -> 0
Example 2:
Input: [2, 2, -1, 2]
Output: true
Explanation: The array has a cycle among indices: 1 -> 3 -> 1
Example 3:
Input: [2, 1, -1, -2]
Output: false
Explanation: The array does not have any cycle.
'''
def circular_array_loop_exists(arr):
current = 0
start = 0
for i in range(len(arr)):
start, current = i, i
while True:
if arr[current] > 0 and arr[start] > 0:
if current + arr[current] > len(arr) - 1:
difference = current + arr[current] - len(arr) - 1
# because we array starts at 0
current = difference - 1
else:
current += arr[current]
elif arr[current] < 0 and arr[start] < 0:
current += arr[current]
if current < 0:
current = len(arr) + current
else:
break
if start == current:
return True
return False
def main():
print(circular_array_loop_exists([1, 2, -1, 2, 2]))
print(circular_array_loop_exists([2, 2, -1, 2]))
print(circular_array_loop_exists([2, 1, -1, -2]))
main()