Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to detect loop in a LinkedList
What is a LinkedList?
A Linked list is a sequence of data structures where each node contains two parts as follows -
- Data: The value or information stored in the node.
- Next: A reference (or pointer) to the next node in the sequence.
Detecting a Loop in a LinkedList
A loop or cycle means that the last node of a linked list is connected back to one of the nodes earlier in the list, creating a cycle. Or, the next pointer of a node points to itself. The following are the ways to detect a loop or cycle in a linked list:
Using HashSet
The HashSet class in Java implements the Set interface; it internally uses a Hash Table to store data. To detect loops in a LinkedList.
- We need to keep track of the nodes we have already visited by storing them in a HashSet object.
- As we traverse the linked list, we need to check if the current node is already in the HashSet.
- If it exists, then we have found a loop.
Example
In the following example, we will create a linked list and then use a HashSet to detect if there is a loop or cycle in the linked list.
import java.util.HashSet;
import java.util.Set;
public class DetectLoop {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
static boolean checkLoop(Node head) {
Set<Node> visitedNode = new HashSet<>();
Node current = head;
while (current != null) {
if(visitedNode.contains(current)){
return true;
}
visitedNode.add(current);
current = current.next;
}
return false;
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = head.next;
if (checkLoop(head)) {
System.out.println("Loop detected in the linked list.");
} else {
System.out.println("No loop detected in the linked list.");
}
}
}
Output
Following is the output of the above code:
Loop detected in the linked list.
Using Floyd's Cycle-Finding Algorithm
Floyd's Cycle-Finding Algorithm, also known as the Tortoise and Hare algorithm. It uses two pointers fast pointer and a slow pointer. The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. If there is a loop, the fast pointer will eventually meet the slow pointer. Following are the steps -
- Initialize two pointers, slow and fast, both pointing to the head of the linked list.
- Move the slow pointer one step forward and the fast pointer two steps forward in each iteration.
- If the slow pointer and fast pointer meet at any point, then there is a loop in the linked list.
- If the fast pointer reaches the end of the linked list (i.e., fast or fast.next is null), then there is no loop.
Example
In the following example, we will create a linked list and then use Floyd's Cycle-Finding Algorithm to detect if there is a loop in the linked list.
public class DetectLoop {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
static boolean checkLoop(Node head){
Node slow = head;
Node fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
return true;
}
}
return false;
}
public static void main(String[] args){
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = head.next;
if (checkLoop(head)) {
System.out.println("Loop detected in the linked list.");
} else {
System.out.println("No loop detected in the linked list.");
}
}
}
Output
Following is the output of the above code:
Loop detected in the linked list.