Skip to content

Commit 9b90628

Browse files
authored
Fix incorrect file extension (Fixes: TheAlgorithms#2716) (TheAlgorithms#2717)
1 parent 64513ff commit 9b90628

File tree

3 files changed

+96
-108
lines changed

3 files changed

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

DataStructures/Lists/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ The `next` variable points to the next node in the data structure and value stor
2525
1. `CircleLinkedList.java` : A circular linked list where next pointer of last node points to first nide of linked list.
2626
2. `SinglyLinkedList.java` : The classic case of single links.
2727
3. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list.
28-
4. `detect_and_create_loop.java` : Detect a loop in linked list
28+
4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list.
2929
5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node.
3030
6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list).

DataStructures/Lists/detect_and_create_loop.jav

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)