Dark Mode On/Off

# Java Program to detect a Loop in a Linked List

In this tutorial, we will see how to detect a loop in a linked list in java. LinkedList is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. But before moving further, if you are not familiar with the concept of the linked list in java, then do check the article on Linked List in Java.

Input: Enter the Linked List elements: 6 7 8 4 5

Output: Loop Found

This can be done by using the following methods:

Approach 1: Using Hash-Set

Approach 2: Without Using Hash-Set

Let us look at each of these approaches for a better understanding.

## Program 1: Detect a Loop in a Linked List

In this program, we will see how to detect a loop in a linked list using hashing approach.

### Algorithm:

1. Start
3. Add elements to the list.
4. Use a boolean method to check if a loop exists or not.
5. Use a HashSet for the same.
6. Traverse the list one by one and keep putting the node addresses in a Hash Table.
7. At any point, if null is reached then return false.
8. If the next current node points to any of the previously stored nodes in HashSet then return true.
9. Display the result.
10. Stop

Let us look at the below example for a better understanding of the above algorithm.

``````// Java program to detect loop in a linked list
import java.util.*;
{
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
{
Node newNode = new Node(newData);
}
static boolean checkLoop(Node n)
{
HashSet<Node> hset = new HashSet<Node>();
while (n != null) {
if (hset.contains(n))
return true;
n = n.next;
}
return false;
}
//Driver program
public static void main(String[] args)
{
System.out.println("Loop found");
else
System.out.println("No Loop found");
}
}``````

Loop found

## Program 2: Detect a Loop in a Linked List

In this program, we will see how to detect a loop in a linked list.

### Algorithm:

1. Start
4. Add elements to the list.
5. Use a boolean method to check if a loop exists or not.
6. Declare a temp variable and initialize it to 0.
7. Traverse the linked list and keep marking visited nodes.
8. If a visited node is found again then there is a loop.
9. Display the result.
10. Stop

Let us look at the below example for a better understanding of the above algorithm.

``````// Java program to detect loop in a linked list
import java.util.*;
public class Main
{
static class Node
{
int data;
Node next;
int temp;
};
{
Node newNode = new Node();
newNode.data = newData;
newNode.temp = 0;
}
static boolean detect(Node n)
{
while (n != null)
{
if (n.temp == 1)
return true;
n.temp = 1;
n = n.next;
}
return false;
}
// Driver code
public static void main(String[] args)
{
System.out.print("Loop found");
else
System.out.print("No Loop found");
}
}``````

Loop found

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.