public static Node hasCycle(Node head) {
        Node slow = head;
        Node fast = head;

        while (slow != null && slow.next != null && fast != null
                && fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) { // There is a cycle.
                // Calculates the cycle length.
                int cycleLen = 0;
                do {
                    ++cycleLen;
                    fast = fast.next;
                } while (slow != fast);

                // Tries to find the start of the cycle.
                slow = head;
                fast = head;
                // Fast pointer advances cycleLen first.
                while (cycleLen-- > 0) {
                    fast = fast.next;
                }
                // Both pointers advance at the same time.
                while (slow != fast) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow; // The start of cycle.
            }
        }
        return null; // no cycle.
    }