Medium · Digit Carry
Understand → Visualise → Practice.
Each array is a reversed linked list of decimal digits. Return the reversed-digit list representing their sum.
The lists store digits from least significant, so add matching positions like column addition, carrying into the next node.
At each position, take the digit from each list, or 0 if that list has ended.
total = a + b + carry; output total % 10 and set carry = total // 10.
Continue while either list has nodes or carry is non-zero.
O(max(m, n)) time and O(max(m, n)) space for the result.
Stopping when both lists end forgets a final carry, e.g. 5 + 5 needs an extra 1 node.
Read the full guide first: Linked Lists.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.