"""
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. 转专业后对于C语言补修的一些体会(1)

    在转入软件工程后,原来的C语言程序设计只有三学分,而信息学院的C语言程序设计有四学分.迫于无奈的我只能再补修一遍C语言,自我认为大一对于C语言的学习已经基本足够,但我发现信息学院用的是不一样的书后,对 ...

  2. 【原】mac电脑使用总结

    mac下终端配置(item2+oh-my-zsh)+solarized配色方案:https://www.cnblogs.com/weixuqin/p/7029177.html

  3. Update(Stage5):DMP项目_业务介绍_框架搭建

    DMP (Data Management Platform) 导读 整个课程的内容大致分为如下两个部分 业务介绍 技术实现 对于业务介绍, 比较困难的是理解广告交易过程中各个参与者是干什么的 对于技术 ...

  4. Ajax记载html

  5. 使用阿里云服务器配置frp实现Windows系统RDP内网穿透

    1.frp服务器采用阿里云ecs的centos7.5系统,客户端是台windows10的系统,做一个RDP服务的内网穿透用. 2.首先下载frp到服务器(链接:https://github.com/f ...

  6. 如何用C++读取图片中的像素

    来源:https://bbs.csdn.net/topics/391956973  3楼 #include <iostream> #include <fstream> #inc ...

  7. tcp连接建立和断开

    TCP协议作为传输层主要协议之一,具有面向连接,端到端,可靠的全双工通信,面向字节流的数据传输协议. 1.TCP报文段 虽然TCP面试字节流,但TCP传输的数据单元却是报文段.TCP报文段分为TCP首 ...

  8. <c:foreach>指定循环次数

    <c:forEach begin="0" end="4" var="i"> <c:set var="ans&qu ...

  9. 为kubernetes-dashboard页面增加过期时间,减少登录次数.

    方法很多,最简单的就是登录后,找到Deployments 服务, 右侧界面会出现kubernetes-dashboard的项目,如果没出现,那么在namespace那里选择全部名称空间. ports: ...

  10. 一个DBA的雄心壮志

    math-liu, 一个甲方的DBA,同时他还有另外一个名字“怕提神”.在某甲方工作的他最近这几年如打了鸡血一般工作起来完全由拼命三郎的那股劲,据说能唯一使他感到一丝鼓励和安慰的是以下这两位大牛前辈. ...