Loading...
Loading...
Loading...
A chain of jobs may loop back: the last node's next can point to an earlier node. Given the values and the position that the last node points back to (-1 means it points to nothing), print YES if the list contains a loop and NO otherwise.
Example 1
Input
3 2 0 4 1
Output
YES
Example 2
Input
3 2 0 4 -1
Output
NO
Try the problem first. Open one hint at a time.
Build the nodes in a Python list, link them, then set the last node's next from pos.
Fast and slow pointers meet inside a loop.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor