给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
示例:
解题思路:
将两个链表遍历将相同位置节点值累加,其和过十进一,新链表相对位置节点值取其和的个位数值。需要考虑到两个链表长度不同时遍历方式、链表遍历完成时最后一位是否需要进一位。
Java:
Python3