A functions checks if the given list has cycle