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. [报错]Could not get a resource from the pool

    redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool解决:开启 ...

  2. wz

    chcp 437 monkeyrunner wz.py wz.bat #coding=utf-8 from com.android.monkeyrunner import MonkeyDevice,M ...

  3. eclipse_maven换源

    1. 手动新增一个xml文件: <?xml version="1.0" encoding="UTF-8"?> <settings xmlns= ...

  4. 实战Google深度学习框架-C5-MNIST数字识别问题

    5.1 MNIST数据处理 MNIST是NIST数据集的一个子集,包含60000张图片作为训练数据,10000张作为测试数据,其中每张图片代表0~9中的一个数字,图片大小为28*28(可以用一个28* ...

  5. CSS-Naming-Conventions--BEM

    BEM BEM : Block Element Modifier There are only two hard problems in Computer Sciences:cache invalid ...

  6. extern "C" 含义

    extern "C" 被 extern 限定的函数或变量是 extern 类型的 被 extern "C" 修饰的变量和函数是按照 C 语言方式编译和链接的 e ...

  7. Python线程和协程-day10

    写在前面 上课第10天,打卡: 感谢Egon老师细致入微的讲解,的确有学到东西! 一.线程 1.关于线程的补充 线程:就是一条流水线的执行过程,一条流水线必须属于一个车间: 那这个车间的运行过程就是一 ...

  8. html中src与href的区别

    概述 src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档 ...

  9. 使用yield和send实现简单的协程函数

    使用yield和send实现协程 协程的本质是在一个线程里实现多个任务之间的来回切换,我们使用yield和send可以实现简单的协程 def pro(): print(1) n = yield &qu ...

  10. C#调用C++导出类的一个实例

    一直认为带导出类dll的只有VC自己可以调用,其它编程语言无法调用,今天看到一篇文章才知道自己错了.https://blog.csdn.net/huiyouyongdeyu2011/article/d ...