Skip to content

Commit db10b26

Browse files
authored
Flyd_Cycle_detection
Initial File
1 parent eaab7d3 commit db10b26

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Flyd_Cycle_detection.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Node:
2+
def __init__(Node, data):
3+
Node.data = data
4+
Node.next = None
5+
6+
7+
def detectCycle(head):
8+
fast=head
9+
slow=head
10+
loopexist=0
11+
while(fast!=None and slow!=None and fast.next!=None):
12+
fast=fast.next.next
13+
slow=slow.next
14+
if(fast==slow):
15+
loopexist=1
16+
break
17+
18+
if(loopexist):
19+
slow=head
20+
while(slow!=fast):
21+
slow=slow.next
22+
fast=fast.next
23+
return slow
24+
return None
25+
26+
27+
head = Node('a')
28+
nodeB = Node('b')
29+
nodeC = Node('c')
30+
nodeD = Node('d')
31+
nodeE = Node('e')
32+
nodeF = Node('f')
33+
nodeG = Node('g')
34+
nodeH = Node('h')
35+
nodeI = Node('i')
36+
nodeJ = Node('j')
37+
38+
head.next = nodeB
39+
nodeB.next = nodeC
40+
nodeC.next = nodeD
41+
nodeD.next = nodeE
42+
nodeE.next = nodeF
43+
nodeF.next = nodeG
44+
nodeG.next = nodeH
45+
nodeH.next = nodeI
46+
nodeI.next = nodeJ
47+
nodeJ.next = nodeD
48+
49+
head=detectCycle(head)
50+
print(head.data)

0 commit comments

Comments
 (0)