public static boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        
        ListNode dummyhead = new ListNode(-1);
        dummyhead.next = head;
        
        ListNode p1 = dummyhead, p2 = dummyhead;
        while (true) {
            p1 = p1.next;
            p2 = p2.next;
            if (p2 != null) {
                p2 = p2.next;
            }
            if (p1 == p2) {
                return true;
            }
            if (p2 == null) {
                break;
            }
        }
        return false;
    }