From 9468d0617ac291df6a572f07b23936560e361b56 Mon Sep 17 00:00:00 2001 From: Shivam Shrey <31056227+shivamshrey@users.noreply.github.com> Date: Sat, 23 Oct 2021 23:52:22 +0530 Subject: [PATCH 1/4] Make improvements in #2346 - Make implementation improvements - Make formatting improvements - Fix file extension - Fix typos - Fix syntax errors --- DataStructures/Lists/CreateAndDetectLoop.java | 92 +++++++++++++++ .../Lists/detect_and_create_loop.jav | 107 ------------------ 2 files changed, 92 insertions(+), 107 deletions(-) create mode 100644 DataStructures/Lists/CreateAndDetectLoop.java delete mode 100644 DataStructures/Lists/detect_and_create_loop.jav diff --git a/DataStructures/Lists/CreateAndDetectLoop.java b/DataStructures/Lists/CreateAndDetectLoop.java new file mode 100644 index 000000000000..96e77a2fba7c --- /dev/null +++ b/DataStructures/Lists/CreateAndDetectLoop.java @@ -0,0 +1,92 @@ +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. + * + * @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. + * + * @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/detect_and_create_loop.jav b/DataStructures/Lists/detect_and_create_loop.jav deleted file mode 100644 index a4f391457d13..000000000000 --- 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"); - } -} From 29a7da4f2d43c07bf91304900e7451b65a221e29 Mon Sep 17 00:00:00 2001 From: Shivam Shrey <31056227+shivamshrey@users.noreply.github.com> Date: Sat, 23 Oct 2021 23:58:32 +0530 Subject: [PATCH 2/4] Update File description in README.md #2346 --- DataStructures/Lists/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStructures/Lists/README.md b/DataStructures/Lists/README.md index 544d22277667..813e1a5f84c0 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 From e741b39ab3b09ec4165b9d65501bc97bb569a42b Mon Sep 17 00:00:00 2001 From: Shivam Shrey <31056227+shivamshrey@users.noreply.github.com> Date: Sun, 24 Oct 2021 00:04:25 +0530 Subject: [PATCH 3/4] Add algorithm's Wikipedia URL #2346 --- DataStructures/Lists/CreateAndDetectLoop.java | 1 + 1 file changed, 1 insertion(+) diff --git a/DataStructures/Lists/CreateAndDetectLoop.java b/DataStructures/Lists/CreateAndDetectLoop.java index 96e77a2fba7c..b795d55c0281 100644 --- a/DataStructures/Lists/CreateAndDetectLoop.java +++ b/DataStructures/Lists/CreateAndDetectLoop.java @@ -44,6 +44,7 @@ static void createLoop(Node head, int k) { /** * Detects the presence of a loop in the linked list. + * Wiki: https://en.wikipedia.org/wiki/Cycle_detection * * @param head the head node of the linked list * From c9059d74243f03fd7816ece791e6e3f819b88301 Mon Sep 17 00:00:00 2001 From: Shivam Shrey <31056227+shivamshrey@users.noreply.github.com> Date: Sun, 24 Oct 2021 00:19:56 +0530 Subject: [PATCH 4/4] Add algorithm explanation URLs #2346 (Fixes #2716) --- DataStructures/Lists/CreateAndDetectLoop.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DataStructures/Lists/CreateAndDetectLoop.java b/DataStructures/Lists/CreateAndDetectLoop.java index b795d55c0281..149bf51df700 100644 --- a/DataStructures/Lists/CreateAndDetectLoop.java +++ b/DataStructures/Lists/CreateAndDetectLoop.java @@ -20,7 +20,8 @@ static void printList(Node head) { /** * 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 */ @@ -44,7 +45,8 @@ static void createLoop(Node head, int k) { /** * Detects the presence of a loop in the linked list. - * Wiki: https://en.wikipedia.org/wiki/Cycle_detection + * @see + * Floyd's Cycle Detection Algorithm * * @param head the head node of the linked list *