diff --git a/DataStructures/Lists/CreateAndDetectLoop.java b/DataStructures/Lists/CreateAndDetectLoop.java new file mode 100644 index 00000000..149bf51d --- /dev/null +++ b/DataStructures/Lists/CreateAndDetectLoop.java @@ -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 + * GeeksForGeeks: Make a loop at K-th position + * @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 + * Floyd's Cycle Detection Algorithm + * + * @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(); + } +} diff --git a/DataStructures/Lists/README.md b/DataStructures/Lists/README.md index 544d2227..813e1a5f 100644 --- a/DataStructures/Lists/README.md +++ b/DataStructures/Lists/README.md @@ -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). \ No newline at end of file diff --git a/DataStructures/Lists/detect_and_create_loop.jav b/DataStructures/Lists/detect_and_create_loop.jav deleted file mode 100644 index a4f39145..00000000 --- a/DataStructures/Lists/detect_and_create_loop.jav +++ /dev/null @@ -1,107 +0,0 @@ -import java.util.*; -import java.util.Scanner; -public class LinkedList { - - static Node head; // head of list - - static class Node // Linked list Node - { - int data; //to store value - Node next; //pointer - Node(int d) { - data = d; - next = null; - } - } - - static int countNodes(Node ptr) //Function to count the number of nodes present - { - int count = 0; - while (ptr != null) { - ptr = ptr.next; - count++; - } - return count; - } - - static public void push(int new_data) // Function to inserts a new Node at front of the list - { - - Node new_node = new Node(new_data); //Allocate a pointer/node and store the data - - new_node.next = head; // make next of new Node as head - - head = new_node; // Move the head to point to new Node. - } - - static void printList(Node head, int total_nodes) //function to traverse through the list and print all data values - { - Node curr = head; - int count = 0; - while (count < total_nodes) { - count++; - System.out.print(curr.data + " "); - curr = curr.next; - } - } - - static Node makeloop(Node head_ref, int k) { - Node temp = head_ref; - int count = 1; - while (count < k) //traverrse the list till point is found - { - temp = temp.next; - count++; - } - - Node connected_point = temp; - - while (temp.next != null) // traverse remaining nodes - temp = temp.next; - - temp.next = connected_point; //connect last node to k-th element - return head_ref; - } - - static boolean detectLoop(Node h) //Function to detect loop, retuens true if loop is in linked list else returns false. - { - HashSet < Node > traverse = new HashSet < Node > (); - while (n != null) { - - if (traverse.contains(n)) //if the hash a;ready contains a record of the node encountered true is returned as a loop isdetected - return true; - - traverse.add(n); - n = n.next; - } - return false; - } - - public static void main(String[] args) { - LinkedList l = new LinkedList(); - - Scanner sc = new Scanner(System.in); - - print("Enter elements in the list, to stop entering press any alphabetical key"); - while (true) { - try { - i = sc.nextInt(); - l.push(i); - } catch (Exception e) { - System.out.println("Creating loop for run"); - } - } - System.out.println("Enter the location to generate loop"); - int k = sc.nextInt() - System.out.print("Given list"); - printList(head, total_nodes); - head = makeloop(head, k); - System.out.print("Modified list with loop"); - printList(head, total_nodes); - - if (detectLoop(head)) - System.out.println("Loop found"); - else - System.out.println("No Loop"); - } -}