11package com .thealgorithms .datastructures .lists ;
22
3- import java .util .Scanner ;
4-
53public final class CreateAndDetectLoop {
4+
5+ // Node class representing a single node in the linked list
66 private CreateAndDetectLoop () {
7+ throw new UnsupportedOperationException ("Utility class" );
78 }
9+ static final class Node {
10+ int data ;
11+ Node next ;
812
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 ;
2016 }
2117 }
2218
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);
3130 */
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 ) {
3434 return ;
3535 }
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- }
4236
43- Node connectedPoint = temp ;
37+ Node node1 = head ;
38+ Node node2 = head ;
4439
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 ++;
4746 }
4847
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+ }
5153
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
5260 /**
5361 * Detects the presence of a loop in the linked list.
5462 *
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>
6164 * @return true if loop exists else false
6265 */
6366 static boolean detectLoop (Node head ) {
@@ -67,40 +70,10 @@ static boolean detectLoop(Node head) {
6770 while (fptr != null && fptr .next != null ) {
6871 sptr = sptr .next ;
6972 fptr = fptr .next .next ;
70- if (fptr == sptr ) {
73+ if (sptr == fptr ) {
7174 return true ;
7275 }
7376 }
74-
7577 return false ;
7678 }
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- }
10679}
0 commit comments