Some Java to build and detect cyclic lists