Workspace Sort Linked List Of 0s 1s And 2s
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Sort Linked List of 0s 1s and 2s visualizer

Learn Sort Linked List of 0s 1s and 2s 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 / 6Execution step
Reference example · JavaScript
Inspect reference input
[
  2,
  1,
  0,
  2,
  1
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let b=[[],[],[]];
a.forEach(x=>{
b[x].push(x);
s("Append to matching chain",{
Zero:b[0],One:b[1],Two:b[2]
})
});
return b.flat()
};

Execution canvas

Active Changed
STEP 1

Append to matching chain

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

Zero0 items

Empty at this step.

One0 items

Empty at this step.

Two1 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

[
  2,
  1,
  0,
  2,
  1
]

Execution steps

  1. Append to matching chain

Computed output

[
  0,
  1,
  1,
  2,
  2
]
JavaScript reference implementation
const solve = (a,s)=>{
  let b=[[],[],[]];
  a.forEach(x=>{
    b[x].push(x);
    s("Append to matching chain",{
      Zero:b[0],One:b[1],Two:b[2]
    })
  });
  return b.flat()
};

Continue learning

Practice related DSA questions in the same topic.