*445. Add Two Numbers II
1. 原始题目
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
2. 题目理解
给定两个整数,求和。注意链表每个结点只放一个数字。高位在前。
3. 解题
思路:两个链表分别进栈,然后出栈时相加,注意设置一个临时变量用来存放进位。每次相加时应该是3个值相加:链表a+链表b+进位。
此外注意的是若最高为还有进位,则继续添加新节点。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = []
stack2 = [] newhead = ListNode(0)
while(l1):
stack1.append(l1.val)
l1 = l1.next
while(l2):
stack2.append(l2.val)
l2 = l2.next
p = 0 # 进位
while stack1 or stack2 or p: # 注意这里别丢了 or p命令。如果有进位则还需创建新节点
temp = (stack1.pop() if stack1 else 0) + \
(stack2.pop() if stack2 else 0) + p # 每次的和为两个链表的和+进位
p = temp//10 # 更新进位 node = ListNode(temp%10)
node.next = newhead.next
newhead.next = node return newhead.next
验证:
# 新建链表1
listnode1 = ListNode_handle(None)
s1 = [1,2,3,4,5,6,7,8]
for i in s1:
listnode1.add(i)
listnode1.print_node(listnode1.head) # 新建链表2
listnode2 = ListNode_handle(None)
s2 = [1,5,9,9,9]
for i in s2:
listnode2.add(i)
listnode2.print_node(listnode2.head) s = Solution()
head = s.addTwoNumbers(listnode1.head, listnode2.head)
listnode1.print_node(head)
1 2 3 4 5 6 7 8
1 5 9 9 9
1 2 3 6 1 6 7 7
*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 ...
- 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】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 445. Add Two Numbers II 链表中的数字求和
[抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 445. Add Two Numbers II【Medium】【两个链表求和】
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- bzoj1061 建图 + 最小费用流
https://www.lydsy.com/JudgeOnline/problem.php?id=106152 对于一个点对上多个点,不太容易建图的时候,考虑逆向思考 申奥成功后,布布经过不懈努力,终 ...
- java io系列18之 CharArrayReader(字符数组输入流)
从本章开始,我们开始对java io中的“字符流”进行学习.首先,要学习的是CharArrayReader.学习时,我们先对CharArrayReader有个大致了解,然后深入了解一下它的源码,最后通 ...
- elasticsearch 集成springboot
和jpa类似,很简单,很强大. pom <dependencies> <dependency> <groupId>org.springframework.boot& ...
- 13个.Net开源的网络爬虫
请点击此处输入图片描述 1:.Net开源的跨平台爬虫框架 DotnetSpider Star:430 DotnetSpider这是国人开源的一个跨平台.高性能.轻量级的爬虫软件,采用 C# 开发.目前 ...
- Git(工作区和暂存区概念)
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- jQuery中获取a标签的值
如题,一组相同action的a标签,不同的是a标签的内容为搜索内容.点击页面显示不同的数据 刚开始试过在 a标签中添加 value值 和id 的值,结果在jQuery中获取值均失败! 后来发现,根本不 ...
- Enum入门【原】
package com.bobo.www.cxf.impl; public enum Traffic { Red(1), Green(2), Yellow(3);//必须最前面 private int ...
- IO流--字符流与字节流--File类常用功能
IO流的常用方法: 1: 文件的读取和写入图解: 2:字节流: 读写文件的方法: 一般效率读取: 读取文件: FileInputStream(); 写数据: Fil ...
- impala系列: 同步Hive元数据和收集统计信息
---====================-- Impala 获取hive 的 metadata ---====================Impala 通常和Hive共用同一个metadat ...
- [Windows] [VS] [C] [取得指针所指内存的十六进制形式字符串]
接口定义如下: #include <Windows.h> // 取得指针所指内存的十六进制形式字符串,size指定字节长度 #define Mem_toString(address, si ...