for formal solution from CS

 model this problem to a cyclic graph

 build the adjacent table

 using recursive method to travel though the adjacent table.

 find the max depth of the cyclic
for solution for the issue only
loop though and just recording the max length
both are O(n)