"""
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
"""
"""
与leetcode2 类似。
这两题都是自己做出来的
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def addTwoNumbers(self, l1, l2):
num1, num2 = 0, 0
if l1 and not l2:
return l1
if l2 and not l1:
return l1
p1, p2 = l1, l2 #将链表l1 和 l2 转成数字num1 和num2 求和得num
while p1:
num1 = num1 * 10 + p1.val
p1 = p1.next
while p2:
num2 = num2 * 10 + p2.val
p2 = p2.next
num = num1 + num2
res = ListNode(0) #头结点
end = ListNode(num % 10) #尾结点
res.next = end
end.next = None
num = num // 10
while num: #从链表尾部插入当前结点cur
cur = ListNode(num % 10) #当前结点
res.next = cur
cur.next = end
end = cur
num = num // 10
return res.next

leetcode445 Add Two Numbers II的更多相关文章

  1. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  2. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  3. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  4. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  5. LeetCode 445 Add Two Numbers II

    445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...

  6. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  9. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

随机推荐

  1. tf.app.run()的作用

    tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...

  2. linux和windows系统的区别

    在21世纪的今天,互联网可以说是当代发展最为迅速的行业,举个很简单的例子,现在的我们不论什么年龄阶层,几乎人手都有一部手机,上面的某博,某音,末手等软件,更是受到多数人的热爱,并且人们不仅仅用其来消遣 ...

  3. 20200213springboot日记

    ------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ 数据库管理 L ...

  4. springboot2.0集成RestTemplate

    实际集成 获取restTemplate实例,封装方法 package com.quant.api.utils.restTemplate; import org.springframework.http ...

  5. BigOps自动化运维安装以及所遇故障处理

    本文参考官方文档进行安装,以及在安装中所遇到的问题呈现给大家.废话就不说了,开始安装.一.准备工作:本机系统环境是CentOS 7 x86 64位硬件配置建议物理内存8G+.CPU 4 cores+. ...

  6. ScrollView不设置contentSize属性依然也可以作为底层滚动View(使用masonry设置scrollView的contentSize)

    第一步 //下层的scroolView self.baseScrollView = [[UIScrollView alloc] init]; self.baseScrollView.delegate ...

  7. HTML5中改变了哪些东西?

    HTML5 推出的理由 想要把目前web上存在的各种问题一并解决 Web浏览器之间的兼容性很低 文档结构不够明确 Web应用程序的功能受到了限制 HTML5重新定义了浏览器的统一标准 HTML5 与 ...

  8. 小程序使用scroll-view横向滑动时,flex布局失效问题

    最近在完善以前项目,类目增多,需要进行横向滑动 实现方法1 可以在外盒子scroll-view使用white-space: nowrap来禁止子盒子换行,子盒子使用display: inline-bl ...

  9. HDU1029 简单DP

    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. &quo ...

  10. div display 常用属性

    none:元素不被显示. block:元素将显示为块级元素,此元素前后会带有换行符. inline:行内元素(即一个挨着一个,都在同一行按从左至右的顺序显示,不单独占一行). 参考: http://w ...