Workspace Remove Nth Node From End
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Remove Nth Node from End visualizer

Learn Remove Nth Node from End with a step-by-step Linked Lists visualizer, JavaScript source code and a worked example. Practice rewiring and advanced.

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,
    5
  ],
  "n": 2,
  "mode": "nth"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,n,mode
},s)=>{
let l=0,r=0;
if(mode==="nth"){
r=n;
while(r<a.length){
s("Move gap pointers",{
Nodes:a,Pointers:[l,r]
});
l++;
r++
}
}else{
while(r+1<a.length){
l++;
r+=2;
s("Move slow and fast",{
Nodes:a,Pointers:[l,r]
})
}
}
a.splice(l,1);
s("Bypass target node",{
Nodes:a
});
return a
};

Execution canvas

Active Changed
STEP 1

Move gap pointers

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

Nodes5 items
Pointers2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Linked Lists · Rewiring and advanced

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,
    5
  ],
  "n": 2,
  "mode": "nth"
}

Execution steps

  1. Move gap pointers
  2. Bypass target node

Computed output

[
  1,
  2,
  3,
  5
]
JavaScript reference implementation
const solve = ({
  a,n,mode
},s)=>{
  let l=0,r=0;
  if(mode==="nth"){
    r=n;
    while(r<a.length){
      s("Move gap pointers",{
        Nodes:a,Pointers:[l,r]
      });
      l++;
      r++
    }
  }else{
    while(r+1<a.length){
      l++;
      r+=2;
      s("Move slow and fast",{
        Nodes:a,Pointers:[l,r]
      })
    }
  }
  a.splice(l,1);
  s("Bypass target node",{
    Nodes:a
  });
  return a
};

Continue learning

Practice related DSA questions in the same topic.