1
1
package com .thealgorithms .datastructures .lists ;
2
2
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
+ */
3
9
public final class CreateAndDetectLoop {
4
10
5
- // Node class representing a single node in the linked list
11
+ /**
12
+ * Private constructor to prevent instantiation of this utility class.
13
+ */
6
14
private CreateAndDetectLoop () {
7
15
throw new UnsupportedOperationException ("Utility class" );
8
16
}
17
+
18
+ /**
19
+ * Node represents an individual element in the linked list, containing
20
+ * data and a reference to the next node.
21
+ */
9
22
static final class Node {
10
23
int data ;
11
24
Node next ;
@@ -16,19 +29,16 @@ static final class Node {
16
29
}
17
30
}
18
31
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
30
41
*/
31
-
32
42
static void createLoop (Node head , int position1 , int position2 ) {
33
43
if (position1 == 0 || position2 == 0 ) {
34
44
return ;
@@ -39,29 +49,32 @@ static void createLoop(Node head, int position1, int position2) {
39
49
40
50
int count1 = 1 ;
41
51
int count2 = 1 ;
42
- // Traverse to find node at position1
52
+ // Traverse to the node at position1
43
53
while (count1 < position1 && node1 != null ) {
44
54
node1 = node1 .next ;
45
55
count1 ++;
46
56
}
47
57
48
- // Traverse to find node at position2
58
+ // Traverse to the node at position2
49
59
while (count2 < position2 && node2 != null ) {
50
60
node2 = node2 .next ;
51
61
count2 ++;
52
62
}
53
63
54
- // Create a loop by connecting node2's next to node1
64
+ // If both nodes are valid, create the loop
55
65
if (node1 != null && node2 != null ) {
56
66
node2 .next = node1 ;
57
67
}
58
68
}
59
- // Method to detect a loop in the linked list
69
+
60
70
/**
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.
62
73
*
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>
65
78
*/
66
79
static boolean detectLoop (Node head ) {
67
80
Node sptr = head ;
0 commit comments