-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd_cycle_detection.py
61 lines (50 loc) · 1.4 KB
/
floyd_cycle_detection.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
"""
Floyd’s Cycle-Finding Algorithm is used to detect loops in linked lists.
Time Complexity: O(n)
Space Complexity: O(1)
"""
class Node:
def __init__(self, data): # data -> value stored in node
self.data = data
self.next = None
def detect_loop(head):
if head is None or head.next is None:
return False
slow = head
fast = head
while fast is not None and fast.next is not None and fast.next.next is not None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
# Linked List Class
class LinkedList:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def add(self, pos):
last = self.head
while last.next:
last = last.next
pos = pos - 1
temp = self.head
while pos:
temp = temp.next
pos -= 1
last.next = temp
if __name__ == '__main__':
t = int(input())
for cases in range(t):
n = int(input())
a = LinkedList() # create a new linked list 'a'.
nodes_a = list(map(int, input().strip().split()))
for x in nodes_a:
a.push(x)
loop = int(input())
if (loop):
a.add(loop)
print(detect_loop(a.head)) # Linked List Class