public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        //find if exist cycle
        ListNode p1 = head;
        ListNode p2 = head;
        while (p1 != null && (p2 != null && p2.next != null)) {
            p1 = p1.next;
            p2 = p2.next.next;
            if (p1 == p2) {
                break;
            }
        }

        boolean existCycle = (p1 == p2);
        if (!existCycle) {
            return null;
        }
        p1 = head;
        while (p1 != p2) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1;
    }