【题目描述】

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.

Example 1:

  • Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)

  • Output: 7 -> 0 -> 8

Note:

You may assume the two numbers do not contain any leading zero, except the number 0 itself.




【题目翻译】

给定两个链表,非负元素从低位向高位排列,如(2 -> 4 -> 3)表示342。求两个链表对应数据之和并从低位到高位输出。

示例:

  • 输入: (2 -> 4 -> 3) + (5 -> 6 -> 4)

  • 输出: 7 -> 0 -> 8


注意:除链表对应数据为零外,数据首位元素不为零



【解题思路】

  • 模拟十进制加法运算,从低位到高位两个链表l1,l2公共部分依次相加求和,链表l3为对应两个元素的和,等于本位相加再加上进位。

  • 当本位之和加进位大于10时,进位为1,保留本位之和加进位减去10作为l3本位。否则,进位为零,本位之和加进位作为l3的本位

  • 最后对于两个链表中长的那一个,将最低位加上进位继续求和后链接到l3后面作为输出即可。




【本题答案】


【每天一道编程系列-2018.2.2】(Ans)

相关文章: