At outer loop node 1
, we'd inner loop through 1
.
At outer loop node 3
, we'd inner loop through 1 -> 3
.
At outer loop node 5
, we'd inner loop through 1 -> 3 -> 5
.
At outer loop node 3
again, we'd inner loop through 1 -> 3 -> 5 -> 3
.
So the brute force way to do it is to conduct a nested traversal-- we can traverse all the nodes, and then at each iteration, traverse through the nodes already visited by the outer loop. In the inner loop, if we encounter a node twice, we know it's been repeated.