Back to questionsDSA Visualizers: 388 Coding Questions Length of Linked List Cycle visualizer Learn Length of Linked List Cycle with a step-by-step Linked Lists visualizer, JavaScript source code and a worked example. Practice singly and doubly linked lists.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 6 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
3,
2,
0,
-4
],
"entry": 1,
"mode": "length"
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,entry,mode
3 },s)=>{
4 let next=a.map((_,i)=>i+1 <a.length?i+1 :entry),l=0 ,r=0 ;
5 do{
6 l=next[l];
7 r=next[next[r]];
8 s("Move slow and fast pointers",{
9 Nodes:a.map((v,i)=>v+" → "+next[i]),Pointers:[l,r]
10 })
11 }
12 while (l!==r);
13 if (mode==="has")return true ;
14 if (mode==="length"){
15 let n=1 ,i=next[l];
16 while (i!==l){
17 n++;
18 s("Count cycle link",{
19 CycleLength:[n],Current:[i]
20 });
21 i=next[i]
22 }
23 return n
24 }
25 l=0 ;
26 while (l!==r){
27 l=next[l];
28 r=next[r];
29 s("Move both one link",{
30 Pointers:[l,r]
31 })
32 }
33 return l
34 };
Execution canvas Active ChangedSTEP 1 Move slow and fast pointers Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
3 → 1 0 2 → 2 1 0 → 3 2 -4 → 1 3
Worked example and algorithm walkthrough Topic: Linked Lists · Singly and doubly linked lists
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input {
"a": [
3,
2,
0,
-4
],
"entry": 1,
"mode": "length"
} Execution steps Move slow and fast pointers Count cycle link Computed output 3 JavaScript reference implementation const solve = ({
a,entry,mode
},s)=>{
let next=a.map((_,i)=>i+1<a.length?i+1:entry),l=0,r=0;
do{
l=next[l];
r=next[next[r]];
s("Move slow and fast pointers",{
Nodes:a.map((v,i)=>v+" → "+next[i]),Pointers:[l,r]
})
}
while(l!==r);
if(mode==="has")return true;
if(mode==="length"){
let n=1,i=next[l];
while(i!==l){
n++;
s("Count cycle link",{
CycleLength:[n],Current:[i]
});
i=next[i]
}
return n
}
l=0;
while(l!==r){
l=next[l];
r=next[r];
s("Move both one link",{
Pointers:[l,r]
})
}
return l
};Continue learning Practice related DSA questions in the same topic.