1
1
package com .thealgorithms .datastructures .lists ;
2
2
3
- import java .util .Scanner ;
4
3
5
- public final class CreateAndDetectLoop {
6
- private CreateAndDetectLoop () {
7
- }
4
+ public class CreateAndDetectLoop {
8
5
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 ;
6
+ // Node class representing a single node in the linked list
7
+ public static class Node {
8
+ int data ;
9
+ Node next ;
16
10
17
- while ( cur != null ) {
18
- System . out . print ( cur . value + " " ) ;
19
- cur = cur . next ;
11
+ Node ( int data ) {
12
+ this . data = data ;
13
+ next = null ;
20
14
}
21
15
}
22
16
17
+ // Method to create a loop between two specific positions in the linked list
23
18
/**
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
+ * Test case that shows the Cycle(loop) in a LinkedList
20
+ * Let's take this linked list:
21
+ * 1->2->3->4->5->6
22
+ * \______/
23
+ * In this linked list we can see there is a cycle.
24
+ * we can create loop by calling createLoop function in main after creating LL
25
+ * createLoop(head,2,5);
26
+ * to detect there is loop or not we can call detectloop function in main
27
+ * detectloop(head);
31
28
*/
32
- static void createLoop (Node head , int k ) {
33
- if (head == null ) {
29
+
30
+ static void createLoop (Node head , int position1 , int position2 ) {
31
+ if (position1 == 0 || position2 == 0 ) {
34
32
return ;
35
33
}
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
34
43
- Node connectedPoint = temp ;
35
+ Node node1 = head ; // node at position1
36
+ Node node2 = head ; // node at position2
37
+
38
+ int count1 = 1 , count2 = 1 ;
39
+
40
+ // Traverse to find node at position1
41
+ while (count1 < position1 && node1 != null ) {
42
+ node1 = node1 .next ;
43
+ count1 ++;
44
+ }
44
45
45
- while (temp .next != null ) { // Traverse remaining nodes
46
- temp = temp .next ;
46
+ // Traverse to find node at position2
47
+ while (count2 < position2 && node2 != null ) {
48
+ node2 = node2 .next ;
49
+ count2 ++;
47
50
}
48
51
49
- temp .next = connectedPoint ; // Connect last node to k-th element
52
+ // Create a loop by connecting node2's next to node1
53
+ if (node1 != null && node2 != null ) {
54
+ node2 .next = node1 ;
55
+ }
50
56
}
51
57
58
+ // Method to detect a loop in the linked list
52
59
/**
53
60
* Detects the presence of a loop in the linked list.
54
61
*
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
- *
62
+ * @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
61
63
* @return true if loop exists else false
62
64
*/
63
65
static boolean detectLoop (Node head ) {
@@ -67,40 +69,13 @@ static boolean detectLoop(Node head) {
67
69
while (fptr != null && fptr .next != null ) {
68
70
sptr = sptr .next ;
69
71
fptr = fptr .next .next ;
70
- if (fptr == sptr ) {
72
+ if (sptr == fptr ) {
71
73
return true ;
72
74
}
73
75
}
74
-
75
76
return false ;
76
77
}
78
+ }
77
79
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
80
98
- if (detectLoop (singlyLinkedList .getHead ())) {
99
- System .out .println ("Loop found" );
100
- } else {
101
- System .out .println ("No loop found" );
102
- }
103
81
104
- sc .close ();
105
- }
106
- }
0 commit comments