Linked List Cycle II - Two Pointers / Cycle Finding

https://algo.monster/problems/linked_list_cycle_ii

I think you misplaced the spot of linked list cycle II and III.

Can you elaborate?

When measuring the size of the loop, wouldn’t it be easier to let the hare stay and the tortoise continue move until they meet?

C++ template build_list has bugs.
It does not generate a real loop.

Thank you for pointing out. We fixed this.

That’s what I think, just loop once and you find the answer

I implemented the same algorithm for both Java and Javascript version, can’t get Javascript version work!
Just wondering what’s wrong with the JS version!

I posted an explanation in the first “Linked List Cycle” problem since it has the same issue: the buildList setup function is broken in Javascript. Here is an updated buildList logic that you can use based on the Python and Java setup logic that will allow tests to pass correctly for valid code:


function buildList(nodes, f) {
    const rawList = Array.from(nodes).map(Number);
    
    const nodeList = [];
    for (let i = 0; i < rawList.length; i++) {
        nodeList.push(new Node(i));
    }
    
    for (let i = 0; i < rawList.length; i++) {
        if (rawList[i] != -1) {
            nodeList[i].next = nodeList[rawList[i]];
        }
    }
    
    return nodeList[0];
}

This is exactly, what I thought as well. Why bother with all that remainder mod logic overhead when you can just move the tortoise until it meets the hare again? Results in much fewer moves of tortoise + hare, less complex code etc. I see no downside