11package com .thealgorithms .datastructures .lists ;
22
3+ /**
4+ * CreateAndDetectLoop provides utility methods for creating and detecting loops
5+ * (cycles) in a singly linked list. Loops in a linked list are created by
6+ * connecting the "next" pointer of one node to a previous node in the list,
7+ * forming a cycle.
8+ */
39public final class CreateAndDetectLoop {
410
5- // Node class representing a single node in the linked list
11+ /**
12+ * Private constructor to prevent instantiation of this utility class.
13+ */
614 private CreateAndDetectLoop () {
715 throw new UnsupportedOperationException ("Utility class" );
816 }
17+
18+ /**
19+ * Node represents an individual element in the linked list, containing
20+ * data and a reference to the next node.
21+ */
922 static final class Node {
1023 int data ;
1124 Node next ;
@@ -16,19 +29,16 @@ static final class Node {
1629 }
1730 }
1831
19- // Method to create a loop between two specific positions in the linked list
20- /*
21- * Test case that shows the Cycle(loop) in a LinkedList
22- * Let's take this linked list:
23- * 1->2->3->4->5->6
24- * \______/
25- * In this linked list we can see there is a cycle.
26- * we can create loop by calling createLoop function in main after creating LL
27- * createLoop(head,2,5);
28- * to detect there is loop or not we can call detectloop function in main
29- * detectloop(head);
32+ /**
33+ * Creates a loop in a linked list by connecting the next pointer of a node
34+ * at a specified starting position (position2) to another node at a specified
35+ * destination position (position1). If either position is invalid, no loop
36+ * will be created.
37+ *
38+ * @param head the head node of the linked list
39+ * @param position1 the position in the list where the loop should end
40+ * @param position2 the position in the list where the loop should start
3041 */
31-
3242 static void createLoop (Node head , int position1 , int position2 ) {
3343 if (position1 == 0 || position2 == 0 ) {
3444 return ;
@@ -39,29 +49,32 @@ static void createLoop(Node head, int position1, int position2) {
3949
4050 int count1 = 1 ;
4151 int count2 = 1 ;
42- // Traverse to find node at position1
52+ // Traverse to the node at position1
4353 while (count1 < position1 && node1 != null ) {
4454 node1 = node1 .next ;
4555 count1 ++;
4656 }
4757
48- // Traverse to find node at position2
58+ // Traverse to the node at position2
4959 while (count2 < position2 && node2 != null ) {
5060 node2 = node2 .next ;
5161 count2 ++;
5262 }
5363
54- // Create a loop by connecting node2's next to node1
64+ // If both nodes are valid, create the loop
5565 if (node1 != null && node2 != null ) {
5666 node2 .next = node1 ;
5767 }
5868 }
59- // Method to detect a loop in the linked list
69+
6070 /**
61- * Detects the presence of a loop in the linked list.
71+ * Detects the presence of a loop in the linked list using Floyd's cycle-finding
72+ * algorithm, also known as the "tortoise and hare" method.
6273 *
63- * @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
64- * @return true if loop exists else false
74+ * @param head the head node of the linked list
75+ * @return true if a loop is detected, false otherwise
76+ * @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
77+ * Floyd's Cycle Detection Algorithm</a>
6578 */
6679 static boolean detectLoop (Node head ) {
6780 Node sptr = head ;
0 commit comments