mediumLinked ListAmazonMicrosoftApple
Digit Chain Sum
Two very large non-negative integers are stored one digit per node, least-significant digit first. Produce their sum in the same format.
Given two linked lists `a` and `b` of digits (LSB first), return a linked list of the sum.
Choose approach
2
a
4
3
5
b
6
4
nil
1/5
Narrationstep 1 of 5
Digits are stored least-significant first → walk both chains in lockstep, add digits + carry.
Variables
aLen=3
bLen=3
How to think about it
- LSB-first makes addition easy: no need to align from the right.
- A dummy head simplifies the first append.
- Loop while any input remains OR carry is non-zero.
- Terminating carry → one extra MSB node.
Edge cases to catch in interview
- Unequal lengths — treat missing digits as 0.
- Final carry becomes an extra MSB.
- Either list empty → the other (possibly with carry).
Examples
Example 1
[7,0,8]{"aVals":[2,4,3],"bVals":[5,6,4]}
342 + 465 = 807.
Example 2
[8,9,9,9,0,0,0,1]{"aVals":[9,9,9,9,9,9,9],"bVals":[9,9,9,9]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed