打卡,第7天
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
从示例来看,这里的digits
应该是倒过来的,即2->4->3
表示的是342
如果它不是倒过来的话,我们可能还需要用栈去将元素取出来。
虽然这是一道Medium
的题目,但是难度其实很小,思路大概是:
将当期指针所指向的值相加得到一个数x
,那么x%10
就是这个位应该为的数,x/10
就是进位,所以算法思路很简单:
dicuss
中还有一个更精炼的写法: