Java Program to Detect loop in a LinkedList
In this example, we will learn to detect whether there is a loop in LinkedList in Java.
To understand this example, you should have the knowledge of the following Java programming topics:
- Java LinkedList
- Java Methods
Example: Detect Loop in a LinkedList
class LinkedList {
// create an object of Node class
// represent the head of the linked list
Node head;
// static inner class
static class Node {
int value;
// connect each node to next node
Node next;
Node[int d] {
value = d;
next = null;
}
}
// check if loop is present
public boolean checkLoop[] {
// create two references at start of LinkedList
Node first = head;
Node second = head;
while[first != null && first.next !=null] {
// move first reference by 2 nodes
first = first.next.next;
// move second reference by 1 node
second = second.next;
// if two references meet
// then there is a loop
if[first == second] {
return true;
}
}
return false;
}
public static void main[String[] args] {
// create an object of LinkedList
LinkedList linkedList = new LinkedList[];
// assign values to each linked list node
linkedList.head = new Node[1];
Node second = new Node[2];
Node third = new Node[3];
Node fourth = new Node[4];
// connect each node of linked list to next node
linkedList.head.next = second;
second.next = third;
third.next = fourth;
// make loop in LinkedList
fourth.next = second;
// printing node-value
System.out.print["LinkedList: "];
int i = 1;
while [i