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的更多相关文章

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

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

  2. 445. Add Two Numbers II - LeetCode

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

  3. LeetCode 445 Add Two Numbers II

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

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

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

  5. 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 ...

  6. 445. Add Two Numbers II 链表中的数字求和

    [抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...

  7. [leetcode]445. Add Two Numbers II 两数相加II

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

  8. 【Leetcode】445. Add Two Numbers II

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

  9. 445. Add Two Numbers II【Medium】【两个链表求和】

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

随机推荐

  1. nlp知识

    1.词集模型 将每个词的出现与否作为一个特征,不考虑词频.也就是一个词在文本在文本中出现1次和多次特征处理是一样的. 2.词袋模型 与词集相比,会考虑词频 sklearn中 CountVectoriz ...

  2. MySQL 8.0.14 新的密码认证方式和客户端链接

    MySQL 8.0.14 新的密码认证方式和客户端链接 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   MySQL8.0在密码认证方式发生了改变,这也是有点小伙伴在MySQL创建 ...

  3. idea创建父子工程

    第一步:创建一个新的父工程father:file—–>new—->project ,注意要选maven,Create from archetype不要勾选.next填写GroupId .A ...

  4. rpmbuild打包php

    安装php依赖库 mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} php有一个依赖库,在yum源于epel源中都没有需要自己打包libico ...

  5. 细说log4j之log4j 2.x

    官网:https://logging.apache.org/log4j/2.x/ 1. 主要组件: 从图中可以看出,log4j2中的主要组件为:Filter,Appender,Logger,他们的层次 ...

  6. SpringBoot系列: Spring MVC视图方法的补充

    SpringMVC 视图方法的参数, 已经在这个文章中写得非常清楚了, 链接为 https://www.cnblogs.com/morethink/p/8028664.html 这篇文章做一些补充. ...

  7. HDB3编码器

    一.HDB3 码介绍 三阶高密度双极性码(英语:High Density Bipolar of Order 3 code,简称:HDB3码)是一种适用于基带传输的编码方式.它是一种 AMI 码的改进型 ...

  8. 【bzoj 1901】Zju2112 Dynamic Rankings

    Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...

  9. node.js 环境

    Centos 7.2 安装 Node.js 环境 Node.js 是运行在服务端的 JavaScript, 是基于 Chrome JavaScript V8 引擎建立的平台. 1. Node.js w ...

  10. 关于"Linux下使用Windows应用程序的尝试"总结

    首推 Flatpak .Flatpak爽啊,命令行启动能不爽吗!? 其他的: 0. AppImage:AppImage试了下,唉,启动TIM时就没反应,其他的应用没试过 1. crossover:收费 ...