I think that depth first search solves it. If an unexplored edge leads to a node visited before, then the graph contains a cycle. This condition also makes it O(n), since you can explore maximum n edges without setting it to true or being left with no unexplored edges.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…