Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Finder {
- public static Node getCycleStart(Node start) {
- ///Floyd's cycle detection algorithm
- Node fast = start;
- Node slow = start;
- while (fast.next != null) {
- slow = slow.next;
- fast = fast.next.next;
- if (slow == fast) {
- break;
- }
- }
- if (fast.next == null) {
- return null;
- }
- slow = start;
- while (slow != fast) {
- slow = slow.next;
- fast = fast.next;
- }
- return fast;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement