We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Inside the while loop if there's a node that has been visited before, we return true as the list contains a cylce:
if(nodesVisited.contains(hare))returntrue;
If hare beceomes null in one point, it means that the list has been fully visited (there's no cylce in it), so the while loop brakes. The last line runs only if the while broke, which happens only if there's no cycle in the list.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cycle Detection
You are viewing a single comment's thread. Return to all comments →
Inside the while loop if there's a node that has been visited before, we return true as the list contains a cylce:
If hare beceomes null in one point, it means that the list has been fully visited (there's no cylce in it), so the while loop brakes. The last line runs only if the while broke, which happens only if there's no cycle in the list.