You are viewing a single comment's thread. Return to all comments →
Something that works with HashSet and it is simple. Also it doesn't modifies the head, which is good.
if( head == null ) { return false; } Set<Node> visited = new HashSet<>(); Node i = head; while( i != null) { visited.add(i); i = i.next; if(visited.contains(i)) { return true; } } return false;
Seems like cookies are disabled on this browser, please enable them to open this website
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
Something that works with HashSet and it is simple. Also it doesn't modifies the head, which is good.