Solution 1:
- reverse list-1
- reverse list-2
- find the sum and store it in a new list represented by list-3
- reverse the list.
Solution2 :
You can do better without list reversal. WLOG(Without loss of generality) I'll assume that both lists h of equal length (prepend with 0 if necessary).
Start the addition from left right (from most significant to least significantple digit). You have 3cases, depending of the sum two digits:
Start the addition from left right (from most significant to least significantple digit). You have 3cases, depending of the sum two digits:
- = 9 : keep the nine and increase a
counter - <>counter x nine, write sum, reset
counter - > 9 : increase last digit, write
counterx zero, write sum (modulo 10), reset counter - Add
2 + 1 = 3: case 3.list = (3),counter = 0
- Add
5 + 4 = 9: case 1list = (3),counter = 1
- Add
6 + 3 = 9: case 1list = (3),counter = 2
- Add
8 + 8 = 16: case 3list = (4, 0, 0, 6),counter = 0
- Add
7 + 2 = 9: case 1list = (4, 0, 0, 6),counter = 1
- Add
9 + 0 = 9: case 1list = (4, 0, 0, 6),counter = 2
- Add
4 + 4 = 8: case 2list = (4, 0, 0, 6, 9, 9, 8),counter = 0
I'll work on the following example:
2 568 794 +
1 438 204
--------- =
4 006 998
Hope this will help to understand :)