题目 【英文版】https://leetcode.com/problems/add-two-numbers/ 【中文版】https://leetcode-cn.com/problems/add-two-numbers/ 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆
          题目
【英文版】https://leetcode.com/problems/add-two-numbers/
【中文版】https://leetcode-cn.com/problems/add-two-numbers/
给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0?开头。
 示例
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
Definition for singly-linked list.
class ListNode(object):
??def __init__(self, x):
????self.val = x
????self.next = None
暴力求解
先求出输入链表l1,l2的和(traverse(self,l)),再用取余除10的方法逐个取出和的位数放入链表。
 ★ 时间复杂度:\(O(n)\)
 ★ 空间复杂的:\(O(n)\)
 
Brute Force -python
class Solution(object):
    def traverse(self,l):
        i=1
        sum=0
        while l is not None:
            sum+=l.val*i
            i=i*10
            l=l.next
        return sum
    def addTwoNumbers(self, l1, l2):
        l1_sum=self.traverse(l1)
        l2_sum=self.traverse(l2)
        sum=l1_sum+l2_sum
        remainder = sum % 10
        result=ListNode(remainder)
        sum = int(sum/10)
        pointer=result
        while sum>0:
            remainder=sum%10
            pointer.next=ListNode(remainder)
            pointer=pointer.next
            sum =int(sum/10)
        return result 
 执行用时 :64 ms, 在所有 Python 提交中击败了73.10%的用户 内存消耗 :11.8 MB, 在所有 Python 提交中击败了27.89%的用户 ?
