LinkedList/detect_and_remove_loop library
� Detect and Remove Loop in Linked List — Floyd's cycle detection + repair
Detects a cycle in a singly linked list using Floyd's Tortoise & Hare algorithm
and removes the loop by locating the loop start and severing the connection.
Returns true when a cycle was found and removed; otherwise false.
Contract:
- Inputs:
head(nullable linked list head). - Output: boolean indicating whether a loop was found and removed.
- Error modes: none;
nullinput returnsfalse.
Complexity: Time O(n), Space O(1).
Example:
final head = LinkedListNode.fromList([1,2,3,4,5]);
head!.next!.next!.next!.next!.next = head.next!.next; // create loop
final found = detectAndRemoveLoop(head);
// found == true, loop removed
Functions
-
detectAndRemoveLoop<
T> (LinkedListNode< T> ? head) → bool