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

Flatten Linked List visualizer

Learn Flatten Linked List 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 / 8Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    5,
    7,
    8
  ],
  [
    10,
    20
  ],
  [
    19,
    22
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let o=[],p=a.map(()=>0);
while(true){
let b=-1;
for(let i=0;
i<a.length;
i++)if(p[i]<a[i].length&&(b<0||a[i][p[i]]<a[b][p[b]]))b=i;
if(b<0)break;
o.push(a[b][p[b]++]);
s("Merge next smallest bottom node",{
Lists:a,Pointers:p,Flattened:o
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Merge next smallest bottom node

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

Lists3 rows
Pointers3 items
Flattened1 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

[
  [
    5,
    7,
    8
  ],
  [
    10,
    20
  ],
  [
    19,
    22
  ]
]

Execution steps

  1. Merge next smallest bottom node

Computed output

[
  5,
  7,
  8,
  10,
  19,
  20,
  22
]
JavaScript reference implementation
const solve = (a,s)=>{
  let o=[],p=a.map(()=>0);
  while(true){
    let b=-1;
    for(let i=0;
    i<a.length;
    i++)if(p[i]<a[i].length&&(b<0||a[i][p[i]]<a[b][p[b]]))b=i;
    if(b<0)break;
    o.push(a[b][p[b]++]);
    s("Merge next smallest bottom node",{
      Lists:a,Pointers:p,Flattened:o
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.