forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydAlgo.java
More file actions
18 lines (18 loc) · 712 Bytes
/
FloydAlgo.java
File metadata and controls
18 lines (18 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Linked List is a data structure having link or address to the next and/or previous
node along with some data. Floyd's algorithm is used to find a loop in a liked list.
For this 2 pointers are used a slow pointer which moves by one and a fast pointer which
moves by two. If loop is present the fast and slow pointer will meet at a node thus
helping in loop detection*/
class FloydAlgo {
public boolean findLoop(Node head)
{
Node slow = head, fast = head;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast)
return true;//Loop is found
return false;
}
}