Educative

Wouldn't cycle detection be a valid and more straightforward approach to solving this?

Wouldn’t detecting a cycle yield the same result with less effort?

Yes, you are correct. However, this approach might not work if you want to print out the orderings as well.