Skip to content

Commit 870d1d6

Browse files
author
albinsabu2023
committed
updated CreateAndDetectLoop
1 parent 334bdbb commit 870d1d6

File tree

2 files changed

+43
-144
lines changed

2 files changed

+43
-144
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
import java.util.Scanner;
43

5-
public final class CreateAndDetectLoop {
6-
private CreateAndDetectLoop() {
7-
}
4+
public class CreateAndDetectLoop {
85

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;
1610

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;
2014
}
2115
}
2216

17+
// Method to create a loop between two specific positions in the linked list
2318
/**
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);
3128
*/
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) {
3432
return;
3533
}
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-
}
4234

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+
}
4445

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++;
4750
}
4851

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+
}
5056
}
5157

58+
// Method to detect a loop in the linked list
5259
/**
5360
* Detects the presence of a loop in the linked list.
5461
*
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>
6163
* @return true if loop exists else false
6264
*/
6365
static boolean detectLoop(Node head) {
@@ -67,40 +69,13 @@ static boolean detectLoop(Node head) {
6769
while (fptr != null && fptr.next != null) {
6870
sptr = sptr.next;
6971
fptr = fptr.next.next;
70-
if (fptr == sptr) {
72+
if (sptr == fptr) {
7173
return true;
7274
}
7375
}
74-
7576
return false;
7677
}
78+
}
7779

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);
9780

98-
if (detectLoop(singlyLinkedList.getHead())) {
99-
System.out.println("Loop found");
100-
} else {
101-
System.out.println("No loop found");
102-
}
10381

104-
sc.close();
105-
}
106-
}

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

-76
This file was deleted.

0 commit comments

Comments
 (0)