leetcodealgorithms-templates2-linked-lists
 
public class FastAndSlow {
    
    // Find the middle of a linked list with two pointers.
    // Time: O(n), Space: O(1)
    public static ListNode middleOfList(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
 
    // Determine if the linked list contains a cycle.
    // Time: O(n), Space: O(1)
    public static boolean hasCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }
 
    // Determine if the linked list contains a cycle and
    // return the beginning of the cycle, otherwise return null.
    // Time: O(n), Space: O(1)
    public static ListNode cycleStart(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        ListNode slow2 = head;
        while (slow != slow2) {
            slow = slow.next;
            slow2 = slow2.next;
        }
        return slow;
    }
}