AddTwoNum
You are given two linked lists representing two non-negative numbers. 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.
example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
思路
思路不算复杂,就是按照我们正常的加法竖式计算的方式来完成此题就可以。需要注意的细节有两点:
- 进位值
- 判断list结尾
关于进位值(程序中定义为extra变量)在本题中,如果是list最后两个数字相加的进位,往往会被忽略。第二点,两个list不一定是等长的,所以在遍历的过程中,需要对list结尾进行判断,防止运行时空指针错误。注意这两点,可能就可以正确完成本题了。
coding
|
|
