445:Add Two Numbers II

2017-06-24  本文已影响0人  denuth

Question:

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.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7


题目意思:
两个数字链表形式给出,求和


代码如下:
答题思路,将两个链表存放到两个数组中去,缺失的高位用0填补,每次计算类似加法器,设置一个进位计数器carry,每次取相应位于进位计数器相加求得相应数值与下一个进位,最后需要对进位进行判断,如果大于0则再加一位。
<pre>
/**

吐槽下:lettcode为什么不支持int a[3] = {0}的初始化方式,= =,战战兢兢想着默认会是0,结果不是= =


看了排行第一的解答= = 先把链表翻转= =然后该干啥干啥
我简直是个弱智

上一篇 下一篇

猜你喜欢

热点阅读