链表加法。
题目来源:https://leetcode.com/problems/add-two-numbers
中文版:https://leetcode-cn.com/problems/add-two-numbers/
题目难度:Medium
题目
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 contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
解答1[Java]:
核心思想
代码
1 | class Solution { |
解答2[Java]:递归思路
1 | public class Problem2Solution2 { |
进阶:链表中的数是正向存储的
思路1:给较短链表的长度和较长链表的长度补齐,补的高位节点的值都设为0。