Sum two numbers represented by linked list
Filed under Amazon
Free read 1 of 3Confirm if digits are least-significant first. If so, standard column addition aligns naturally with list traversal from head to tail.
The question
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list in reverse order.
Constraints
- The number of nodes in each linked list is in the range [1, 100].
- 0 <= Node.val <= 9
- The lists do not contain any leading zero, except the number 0 itself.
Examples
- Input
- l1 = [2, 4, 3], l2 = [5, 6, 4]
- Output
- [7, 0, 8]
- Why
- 342 + 465 = 807, which is represented as [7, 0, 8].
- Input
- l1 = [9, 9, 9], l2 = [1]
- Output
- [0, 0, 0, 1]
- Why
- 999 + 1 = 1000, represented as [0, 0, 0, 1].
Approach
Confirm if digits are least-significant first. If so, standard column addition aligns naturally with list traversal from head to tail.
Use a dummy head and track a carry variable. Loop while either pointer is non-null or carry > 0, advancing pointers and adding new nodes.
Define a helper taking two node pointers and carry. Compute sum, create current node, and recurse on `next` pointers with new carry.
Lists of different lengths, one list empty/zero, and continuous carries generating a new most-significant digit (e.g., 99 + 1).
Tips
- What they are testing: Tests list traversal, carry propagation across unequal length lists, and handling a trailing carry.
- Common mistake: Forgetting to append a final node when a carry remains after both lists have been fully traversed.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.