-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_Cyclicity.py
42 lines (33 loc) · 954 Bytes
/
Test_Cyclicity.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
# cycle linked list. Singly linked list returns null if no cycle.
# if cycle exists return the start of the cycle or roothead.
# you dont know length of the list
# [a-->b-->c--> a ] should return a.
# [a-->b-->c] should return null
# visited = {
# a : b
# b : c
# c : a ## flag. The flag is set to true if it points to another key.
# }
# visited = {
# a : b
# b : c
# c : none
# }
# if [key] does not exist visited[key] = something
def TestCyclicity(head):
main_head = head
visited = {}
currHead = head
isCyclicity = False
while currHead:
if currHead not in visited:
visited[currHead] = currHead.next
currHead = currHead.next
elif visited[currHead] in visited:
isCyclicity = True
break
if isCyclicity:
return main_head
else:
return None
# Their approach two seperate numerators and you iterates faster one that is slower