Advertisement
Guest User

Question 2

a guest
May 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. class Finder {
  2.     public static Node getCycleStart(Node start) {
  3.         ///Floyd's cycle detection algorithm
  4.         Node fast = start;
  5.         Node slow = start;
  6.  
  7.         while (fast.next != null) {
  8.             slow = slow.next;
  9.             fast = fast.next.next;
  10.             if (slow == fast) {
  11.                 break;
  12.             }
  13.         }
  14.  
  15.         if (fast.next == null) {
  16.             return null;
  17.         }
  18.  
  19.         slow = start;
  20.         while (slow != fast) {
  21.             slow = slow.next;
  22.             fast = fast.next;
  23.         }
  24.  
  25.         return fast;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement