57. Add Two Numbers — Reversed Digits
Each node stores one decimal digit, beginning with the least significant digit. Add the represented numbers and return the sum in the same reversed-digit representation.
A carry can extend the answer beyond both input lengths. Leave both inputs unchanged and follow the canonical representation rules for zero and leading high-order zeros.
Example 1
Input
A:[2,4,3] (342), B:[5,6,4] (465)
Output
[7,0,8] (807)
Explanation: Column 0 sums to 7; column 1 sums to 10, emits 0 and carries 1; column 2 sums to 8.
Example 2
Input
A:[9,9,9], B:[1]
Output
[0,0,0,1]
Explanation: Carry continues after B ends, then creates a final new most-significant node after both cursors end.
Example 3
Input
A:null, B:null
Output
fresh [0]
Explanation: The do/while emits a canonical zero node even with two empty inputs.
Example 4
Input
A:[1,0], B:[1]
Output
IllegalArgumentException
Explanation: [1,0] is a noncanonical representation of one under this API; the most-significant zero is rejected.
Constraints
0 <= nodes per input list <= 1050 <= node.value <= 9- Digits are least-significant first. Null means zero; no leading zeroes except the single digit zero.
Hints
Hint 1
Reversed digits arrive in exactly the order used by paper-and-pencil column addition.
Hint 2
Use zero for a missing input digit and carry only the quotient by ten.
Hint 3
Append sum%10 into a new node and continue while either cursor or the carry remains.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Carry outlives the shorter input
[9,9,9]
[1]
[0,0,0,1]