Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions DataStructures/Lists/CreateAndDetectLoop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package DataStructures.Lists;

import java.util.Scanner;

public class CreateAndDetectLoop {

/**
* Prints the linked list.
*
* @param head head node of the linked list
*/
static void printList(Node head) {
Node cur = head;

while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
}

/**
* Creates a loop in the linked list.
* @see <a href="https://www.geeksforgeeks.org/make-loop-k-th-position-linked-list/">
* GeeksForGeeks: Make a loop at K-th position</a>
* @param head head node of the linked list
* @param k position of node where loop is to be created
*/
static void createLoop(Node head, int k) {
if (head == null)
return;
Node temp = head;
int count = 1;
while (count < k) { // Traverse the list till the kth node
temp = temp.next;
count++;
}

Node connectedPoint = temp;

while (temp.next != null) // Traverse remaining nodes
temp = temp.next;

temp.next = connectedPoint; // Connect last node to k-th element
}

/**
* Detects the presence of a loop in the linked list.
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
* Floyd's Cycle Detection Algorithm</a>
*
* @param head the head node of the linked list
*
* @return true if loop exists else false
*/
static boolean detectLoop(Node head) {
Node sptr = head;
Node fptr = head;

while (fptr != null && fptr.next != null) {
sptr = sptr.next;
fptr = fptr.next.next;
if (fptr == sptr)
return true;
}

return false;
}

public static void main(String[] args) {
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of elements to be inserted: ");
int n = sc.nextInt();
System.out.printf("Enter the %d elements: \n", n);
while (n-- > 0)
singlyLinkedList.insert(sc.nextInt());

System.out.print("Given list: ");
printList(singlyLinkedList.getHead());
System.out.println();

System.out.println("Enter the location to generate loop: ");
int k = sc.nextInt();

createLoop(singlyLinkedList.getHead(), k);

if (detectLoop(singlyLinkedList.getHead()))
System.out.println("Loop found");
else
System.out.println("No loop found");

sc.close();
}
}
2 changes: 1 addition & 1 deletion DataStructures/Lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ The `next` variable points to the next node in the data structure and value stor
1. `CircleLinkedList.java` : A circular linked list where next pointer of last node points to first nide of linked list.
2. `SinglyLinkedList.java` : The classic case of single links.
3. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list.
4. `detect_and_create_loop.java` : Detect a loop in linked list
4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list.
5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node.
6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list).
107 changes: 0 additions & 107 deletions DataStructures/Lists/detect_and_create_loop.jav

This file was deleted.