leetcode445 Add Two Numbers II
"""
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的更多相关文章
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- maven打jar包(包括依赖jar)
<build> <plugins> <plugin> <artifactId> maven-assembly-plugin </artifactI ...
- 【洛谷P3500】TES-Intelligence Test
前言 先是这位神仙写了这道题 \(O(n\log n)\) 的做法.然后去他的博客上恰了一波. 然后发现这道题有 \(O(n)\) 的做法的.其实也不难. 题目 题目链接:https://www.lu ...
- comsol日常联系
- StaticLinkList(静态链表)
写这个写了几次,然后都没写完就关掉了,所以也不想多码字了,直接上代码吧(本来还认真自制了一张图片来理解静态链表的cursor与sub之间的关系)但其实也就那么回事:通过游标来找下标通过下标找到对应的数 ...
- CircleLinkList(循环链表)
尾插法和循环链表. #include <stdio.h> #include <stdlib.h> typedef struct CircleLinkList { int dat ...
- Java Web 前端资源文件的路径问题
WEB-INF是Java Web应用的安全目录,在部署时用于存放class文件.项目用到的库(jar包).Java Web应用的配置文件web.xml. 浏览器不能访问此目录下的资源,比如在WEB-I ...
- unity 热更方案对比
现在一般使用的方案有:tulua&ulua.xlua.ILRuntime 对比: tulua&ulua 方案成熟,稳定第三方库支持 xlua 之前是为了热更修复线上bug的,腾讯发起的 ...
- 「快学Docker」Docker简介、安装和Hello World实现
前言 Docker已经成为了一门炙手可热的技术,每个程序员(特别是后端程序员)都应该学习下Docker这门技术. Docker是什么 来自官网的定义:Docker是以Docker容器为资源分割和调度的 ...
- SpringBoot之基础入门-专题一
SpringBoot之基础入门-专题一 一.Spring介绍 1.1.SpringBoot简介 在初次学习Spring整合各个第三方框架构建项目的时候,往往会有一大堆的XML文件的配置,众多的dtd或 ...
- 高版本的Hibernate
我的查询语句是“from TA pojo where pojo.tbs.name='tb1'”,可结果报错. 高版本的Hibernate不能这样查Set了, 要改成这样: from TA pojo i ...