Guys can Someone give me O(N) approach for this problem as When I checked around 20-30 solutions all were O(N^2) so I need a code basically as I have some idea that If a node is in cycle then the answer is the node itself and else the answer is nearest node in a cycle.
Please help me implement code for O(N).
P.S. I checked that no blog exists on this problem and Please forgive me as I know this may be a NOOB doubt and I have started learning Graphs a week back only.