Skip to content

Commit cd87dc3

Browse files
authored
Enhance docs, add tests in CreateAndDetectLoop (#5993)
1 parent c8e9e74 commit cd87dc3

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

src/main/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoop.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package 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+
*/
39
public 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;

src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CreateAndDetectLoopTest {
1212

1313
@BeforeEach
1414
void setUp() {
15-
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6
15+
// Set up a linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6
1616
head = new CreateAndDetectLoop.Node(1);
1717
CreateAndDetectLoop.Node second = new CreateAndDetectLoop.Node(2);
1818
CreateAndDetectLoop.Node third = new CreateAndDetectLoop.Node(3);
@@ -44,7 +44,7 @@ void testCreateAndDetectLoopLoopExists() {
4444

4545
@Test
4646
void testCreateLoopInvalidPosition() {
47-
// Create loop with invalid positions
47+
// Create loop with invalid positions (0)
4848
CreateAndDetectLoop.createLoop(head, 0, 0);
4949

5050
// Ensure no loop was created
@@ -62,10 +62,25 @@ void testCreateLoopSelfLoop() {
6262

6363
@Test
6464
void testCreateLoopNoChangeForNonExistentPositions() {
65-
// Create a loop with positions that don't exist in the linked list
65+
// Create a loop with non-existent positions
6666
CreateAndDetectLoop.createLoop(head, 10, 20);
6767

6868
// Ensure no loop was created
6969
assertFalse(CreateAndDetectLoop.detectLoop(head), "No loop should be created if positions are out of bounds.");
7070
}
71+
72+
@Test
73+
void testMultipleNodesWithNoLoop() {
74+
// Multiple nodes without creating any loop
75+
assertFalse(CreateAndDetectLoop.detectLoop(head), "No loop should be detected for a standard linear list.");
76+
}
77+
78+
@Test
79+
void testHeadToTailLoop() {
80+
// Create a loop from the tail back to the head
81+
CreateAndDetectLoop.createLoop(head, 1, 6);
82+
83+
// Detect the head-to-tail loop
84+
assertTrue(CreateAndDetectLoop.detectLoop(head), "A head-to-tail loop should be detected.");
85+
}
7186
}

0 commit comments

Comments
 (0)