public boolean hasCycle(ListNode head) {
            if (head == null)
                return false;

            ListNode slow = head;
            ListNode fast = head;

            while (true) {
                if (slow == null)
                    return false;
                else
                    slow = slow.next;

                if (fast == null || fast.next == null)
                    return false;
                else
                    fast = fast.next.next;

                if (slow == fast)
                    return true;
            }

        }