forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
55 lines (49 loc) · 1.43 KB
/
LinkedList.java
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
//Floyd’s Cycle-Finding Algorithm:- To find a loop in linked list
public class LinkedList {
Node head;
class Node { //creation of a node
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public void push(int new_data) //insertion of a new node
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void detectLoop() //floyd's cycle finding algorithm using two pointers
{
Node slow_p = head, fast_p = head;
int flag = 0;
while (slow_p != null && fast_p != null
&& fast_p.next != null) {
slow_p = slow_p.next;
fast_p = fast_p.next.next;
if (slow_p == fast_p) { //if two pointers are same then there is a loop
flag = 1;
break;
}
}
if (flag == 1)
System.out.println("Loop found");
else
System.out.println("Loop not found");
}
public static void main(String args[]) //main method
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(4);
llist.push(15);
llist.push(10);
llist.head.next.next.next.next = llist.head;
llist.detectLoop();
}
}
//Time Complexity - O(n)
//Space Complexity - O(1)