Workspace Add One To Linked List
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Add One to Linked List visualizer

Learn Add One to 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 / 4Execution step
Reference example · JavaScript
Inspect reference input
[
  1,
  9,
  9
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let c=1;
for(let i=a.length-1;
i>=0&&c;
i--){
let x=a[i]+c;
a[i]=x%10;
c=Math.floor(x/10);
s("Propagate carry",{
Digits:a,Carry:[c]
},[i])
}
if(c)a.unshift(c);
return a
};

Execution canvas

Active Changed
STEP 1

Propagate carry

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

Digits3 items
Carry1 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

[
  1,
  9,
  9
]

Execution steps

  1. Propagate carry

Computed output

[
  2,
  0,
  0
]
JavaScript reference implementation
const solve = (a,s)=>{
  let c=1;
  for(let i=a.length-1;
  i>=0&&c;
  i--){
    let x=a[i]+c;
    a[i]=x%10;
    c=Math.floor(x/10);
    s("Propagate carry",{
      Digits:a,Carry:[c]
    },[i])
  }
  if(c)a.unshift(c);
  return a
};

Continue learning

Practice related DSA questions in the same topic.