Posts

Content- DSA

Linked Lists Floyd Cycle Detection /**  * Code floyds cycle algorithm  * 1. detect the cycle 2. size of cycle 3. start of cycle 4. remove the cycle  * */ package dsa.linkedlist.single; import java.util.HashSet; import java.util.Set; public class FloydCycleLinkedList { public static void main(String[] args) { Node head = null, q = null, head1; FloydCycleLinkedList floydCycleLL = new FloydCycleLinkedList(); // Seed input data for (int i = 1; i < 10; i++) head = floydCycleLL.push(head, i); floydCycleLL.print(head); // Make a circular LL at node 5 // floydCycleLL.linkLastNodeToKthNode(head, 5); floydCycleLL.print(head); // 1. detect the cycle boolean isCycle = floydCycleLL.detectLoop(head); System.out.println("Loop/Cycle exists in the Linked List: " + isCycle); // 2. size of cycle int sizeOfCycle = floydCycleLL.getSizeOfCycle(head); System.out.println("Size of Cycle is: " + sizeOfCycle); // 3. start of cycle Node
Recent posts