A site's redirect rules are stored in an array next. Page i redirects to page next[i], and next[i] === -1 means page i loads normally. Each page has at most one redirect, so following the rules from a page is like walking a linked list where next[i] is the index of the node after node i.
A visitor opens page 0 and follows redirects. Write hasRedirectLoop(next) that returns true if the visitor would be redirected forever (the walk reaches a page it already visited), or false if it reaches a page that loads. Pages the visitor never reaches don't matter. Try to use O(1) extra space.
Input: next = [1,2,3,1] Output: true
Page 0 goes to 1, 2, 3, then back to 1, forever.
Input: next = [1,2,-1] Output: false
Pages 0, 1, 2, and page 2 loads.
1 <= next.length <= 10^4next[i] is -1 or a valid index 0 <= next[i] < next.length.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #235 Detect a Redirect Loop. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.