Workspace Reverse Nodes In K Group
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Reverse Nodes in K Group visualizer

Learn Reverse Nodes in K Group 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 / 3Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    2,
    3,
    4,
    5
  ],
  "k": 2
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
for(let i=0;
i+k<=a.length;
i+=k){
let l=i,r=i+k-1;
while(l<r){
[a[l],a[r]]=[a[r],a[l]];
s("Reverse full group",{
Nodes:a
},[l,r]);
l++;
r--
}
}
return a
};

Execution canvas

Active Changed
STEP 1

Reverse full group

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

Nodes5 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
  ],
  "k": 2
}

Execution steps

  1. Reverse full group

Computed output

[
  2,
  1,
  4,
  3,
  5
]
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  for(let i=0;
  i+k<=a.length;
  i+=k){
    let l=i,r=i+k-1;
    while(l<r){
      [a[l],a[r]]=[a[r],a[l]];
      s("Reverse full group",{
        Nodes:a
      },[l,r]);
      l++;
      r--
    }
  }
  return a
};

Continue learning

Practice related DSA questions in the same topic.