{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "452811b643cd4c9f8f98931e543a0050",
  "keywords": "递归,链表,数学",
  "title": "两数相加",
  "desc": [
    {
      "content": "\n<p>给你两个 <strong>非空</strong> 的链表，表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的，并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加，并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外，这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg\" style=\"width: 483px; height: 342px;\" /><pre><strong>输入：</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出：</strong>[7,0,8]<strong><br />解释：</strong>342 + 465 = 807.</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>l1 = [0], l2 = [0]<strong><br />输出：</strong>[0]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出：</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li>每个链表中的节点数在范围 <code>[1, 100]</code> 内</li>\t<li><code>0 <= Node.val <= 9</code></li>\t<li>题目数据保证列表表示的数字不含前导零</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n\t\tlrr = l1\n\t\twhile True:\n\t\t\tl1.val = l1.val + l2.val\n\t\t\tif l1.next is None and l2.next is None and l1.val < 10:\n\t\t\t\tbreak\n\t\t\tif l1.next is None:\n\t\t\t\tl1.next = ListNode(0)\n\t\t\tif l2.next is None:\n\t\t\t\tl2.next = ListNode(0)\n\t\t\tif l1.val >= 10:\n\t\t\t\tl1.val = l1.val - 10\n\t\t\t\tl1.next.val += 1\n\t\t\tl1 = l1.next\n\t\t\tl2 = l2.next\n\t\treturn lrr\n# %%\nl = LinkList()\nlist1 = [2,4,3]\nlist2 = [5,6,4]\nl1 = l.initList(list1)\nl2 = l.initList(list2)\ns = Solution()\nprint(l.convert_list(s.addTwoNumbers(l1, l2)))",
    "language": "python"
  },
  "node_id": "dailycode-d806ed41ab324a0582401a2c7b2a3da7",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469916"
}