"""
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. 吴裕雄 python 神经网络——TensorFlow ckpt文件保存方法

    import tensorflow as tf v1 = tf.Variable(tf.random_normal([1], stddev=1, seed=1)) v2 = tf.Variable(t ...

  2. springboot pom问题及注解

    springboot pom不需要指定版本号 springboot会自己管理版本号 <!-- 支持热部署 --> <dependency> <groupId>org ...

  3. python搭建后台服务

    后端 # coding:utf-8 # 2019/10/22 16:01 # huihui # ref: from flask import Flask, abort, request, jsonif ...

  4. 短信通道——阿里大鱼(java)

    综述            注:本文写于2017年6月22日升级之后. 使用阿里大鱼发送短信已经成为一种趋势,因为权威,而且价格也比较适中,被越来越多的公司所采用.在介绍阿里大鱼发送短信之前,首先得拥 ...

  5. mcast_set_if函数

    #include <errno.h> #include <string.h> #include <net/if.h> #include <sys/ioctl. ...

  6. SpringBoot 集成Log4j、集成AOP

    集成Log4j (1)在pom.xml中添加依赖 <!--去掉springboot默认的日志--> <dependency> <groupId>org.spring ...

  7. 第四周之Hadoop学习(四)

    上周已经成功完成了Hadoop的学习,这周则是搭建好Hadoop的安卓编程环境 今天的学习根据这篇博客:https://blog.csdn.net/HcJsJqJSSM/article/details ...

  8. 笔记-python-standard library-8.3.collections

    笔记-python-standard library-8.3.collections 1.      collections简介 Source code: Lib/collections/__init ...

  9. PTA的Python练习题(十一)

    从 第4章-3 猴子吃桃问题 继续 1. a=eval(input()) def count(n): b=1 for i in range(n-1): b=(b+1)*2 return b print ...

  10. Struts笔记一

    Struts 概念: 是一个MVC框架: Servlet的缺点 1.在web.xml中文件中需要配置很多行代码,维护起来很不方便呢,不利于团队合作. 2.一个servlet的入口只有一个doPost或 ...