forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyd_Cycle_detection.py
More file actions
50 lines (43 loc) · 951 Bytes
/
Flyd_Cycle_detection.py
File metadata and controls
50 lines (43 loc) · 951 Bytes
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
class Node:
def __init__(Node, data):
Node.data = data
Node.next = None
def detectCycle(head):
fast=head
slow=head
loopexist=0
while(fast!=None and slow!=None and fast.next!=None):
fast=fast.next.next
slow=slow.next
if(fast==slow):
loopexist=1
break
if(loopexist):
slow=head
while(slow!=fast):
slow=slow.next
fast=fast.next
return slow
return None
head = Node('a')
nodeB = Node('b')
nodeC = Node('c')
nodeD = Node('d')
nodeE = Node('e')
nodeF = Node('f')
nodeG = Node('g')
nodeH = Node('h')
nodeI = Node('i')
nodeJ = Node('j')
head.next = nodeB
nodeB.next = nodeC
nodeC.next = nodeD
nodeD.next = nodeE
nodeE.next = nodeF
nodeF.next = nodeG
nodeG.next = nodeH
nodeH.next = nodeI
nodeI.next = nodeJ
nodeJ.next = nodeD
head=detectCycle(head)
print(head.data)