Workspace Length Of Linked List
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Length of Linked List visualizer

Learn Length of Linked List with a step-by-step Linked Lists visualizer, JavaScript source code and a worked example. Practice singly and doubly linked lists.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 5Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    2,
    3,
    4
  ],
  "k": null
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
let next=a.map((_,i)=>i+1<a.length?i+1:-1),i=0,c=0;
while(i>=0){
s("Follow next link",{
Nodes:a.map((v,j)=>v+" → "+next[j]),Current:[i],Count:[c]
});
if(k!==null&&a[i]===k)return i;
c++;
i=next[i]
}
return k===null?c:-1
};

Execution canvas

Active Changed
STEP 1

Follow next link

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Nodes4 items
Current1 items
Count1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

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": [
    1,
    2,
    3,
    4
  ],
  "k": null
}

Execution steps

  1. Follow next link

Computed output

4
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  let next=a.map((_,i)=>i+1<a.length?i+1:-1),i=0,c=0;
  while(i>=0){
    s("Follow next link",{
      Nodes:a.map((v,j)=>v+" → "+next[j]),Current:[i],Count:[c]
    });
    if(k!==null&&a[i]===k)return i;
    c++;
    i=next[i]
  }
  return k===null?c:-1
};

Continue learning

Practice related DSA questions in the same topic.