forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Floyd's_Cycle_Detection.py
40 lines (35 loc) · 1011 Bytes
/
Floyd's_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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to insert node in Linked List
def insert(self, val):
temp = Node(val)
temp.next = self.head
self.head = temp
# Function to detect cycle in Linked List
def detectCycle(self):
slow_ptr = self.head
fast_ptr = self.head
while(fast_ptr and fast_ptr.next):
slow_ptr = slow_ptr.next
fast_ptr = fast_ptr.next.next
if slow_ptr == fast_ptr:
return True
return False
# Driver Code
mylist = LinkedList()
values=[10, 20, 30, 40]
n= len(values)
for i in range(n-1,-1,-1):
mylist.insert(values[i])
# Creating a loop in Linked List
mylist.head.next.next.next.next = mylist.head.next
if mylist.detectCycle():
print("Cycle found")
else:
print("No cycle found")