|
| 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 | +} |
0 commit comments