1
1
package com .thealgorithms .datastructures .lists ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
public final class CreateAndDetectLoop {
4
+
5
+ // Node class representing a single node in the linked list
6
6
private CreateAndDetectLoop () {
7
+ throw new UnsupportedOperationException ("Utility class" );
7
8
}
9
+ static final class Node {
10
+ int data ;
11
+ Node next ;
8
12
9
- /**
10
- * Prints the linked list.
11
- *
12
- * @param head head node of the linked list
13
- */
14
- static void printList (Node head ) {
15
- Node cur = head ;
16
-
17
- while (cur != null ) {
18
- System .out .print (cur .value + " " );
19
- cur = cur .next ;
13
+ Node (int data ) {
14
+ this .data = data ;
15
+ next = null ;
20
16
}
21
17
}
22
18
23
- /**
24
- * Creates a loop in the linked list.
25
- *
26
- * @see
27
- * <a href="https://www.geeksforgeeks.org/make-loop-k-th-position-linked-list/">
28
- * GeeksForGeeks: Make a loop at K-th position</a>
29
- * @param head head node of the linked list
30
- * @param k position of node where loop is to be created
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);
31
30
*/
32
- static void createLoop (Node head , int k ) {
33
- if (head == null ) {
31
+
32
+ static void createLoop (Node head , int position1 , int position2 ) {
33
+ if (position1 == 0 || position2 == 0 ) {
34
34
return ;
35
35
}
36
- Node temp = head ;
37
- int count = 1 ;
38
- while (count < k ) { // Traverse the list till the kth node
39
- temp = temp .next ;
40
- count ++;
41
- }
42
36
43
- Node connectedPoint = temp ;
37
+ Node node1 = head ;
38
+ Node node2 = head ;
44
39
45
- while (temp .next != null ) { // Traverse remaining nodes
46
- temp = temp .next ;
40
+ int count1 = 1 ;
41
+ int count2 = 1 ;
42
+ // Traverse to find node at position1
43
+ while (count1 < position1 && node1 != null ) {
44
+ node1 = node1 .next ;
45
+ count1 ++;
47
46
}
48
47
49
- temp .next = connectedPoint ; // Connect last node to k-th element
50
- }
48
+ // Traverse to find node at position2
49
+ while (count2 < position2 && node2 != null ) {
50
+ node2 = node2 .next ;
51
+ count2 ++;
52
+ }
51
53
54
+ // Create a loop by connecting node2's next to node1
55
+ if (node1 != null && node2 != null ) {
56
+ node2 .next = node1 ;
57
+ }
58
+ }
59
+ // Method to detect a loop in the linked list
52
60
/**
53
61
* Detects the presence of a loop in the linked list.
54
62
*
55
- * @see
56
- * <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
57
- * Floyd's Cycle Detection Algorithm</a>
58
- *
59
- * @param head the head node of the linked list
60
- *
63
+ * @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
61
64
* @return true if loop exists else false
62
65
*/
63
66
static boolean detectLoop (Node head ) {
@@ -67,40 +70,10 @@ static boolean detectLoop(Node head) {
67
70
while (fptr != null && fptr .next != null ) {
68
71
sptr = sptr .next ;
69
72
fptr = fptr .next .next ;
70
- if (fptr == sptr ) {
73
+ if (sptr == fptr ) {
71
74
return true ;
72
75
}
73
76
}
74
-
75
77
return false ;
76
78
}
77
-
78
- public static void main (String [] args ) {
79
- SinglyLinkedList singlyLinkedList = new SinglyLinkedList ();
80
- Scanner sc = new Scanner (System .in );
81
-
82
- System .out .println ("Enter the number of elements to be inserted: " );
83
- int n = sc .nextInt ();
84
- System .out .printf ("Enter the %d elements: %n" , n );
85
- while (n -- > 0 ) {
86
- singlyLinkedList .insert (sc .nextInt ());
87
- }
88
-
89
- System .out .print ("Given list: " );
90
- printList (singlyLinkedList .getHead ());
91
- System .out .println ();
92
-
93
- System .out .println ("Enter the location to generate loop: " );
94
- int k = sc .nextInt ();
95
-
96
- createLoop (singlyLinkedList .getHead (), k );
97
-
98
- if (detectLoop (singlyLinkedList .getHead ())) {
99
- System .out .println ("Loop found" );
100
- } else {
101
- System .out .println ("No loop found" );
102
- }
103
-
104
- sc .close ();
105
- }
106
79
}
0 commit comments