[題目]
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
https://leetcode.com/problems/add-two-numbers-ii/description/
困難版是因為list中的位數是由高位到低位,而非簡單版的低位到高位。
[思路]
其實想法很單純,既然你已經會做簡單版的了,那麼我們把這題的input先變成簡單版的即可。
怎麼變呢? 就是做link list reverse(也是經典題)。
我們先把list 1 & 2 reverse,然後相加,然後把result list做reverse,就得到最終答案了。