[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

最后看第一位是不是0,不是0才能返回。

[思维问题]:

顺序是反的,不知道用stack

通过sum / 10的方法可以把十位取出来,不用清空,每次除10一直加就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

顺序是反的就要用stack求和

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 先初始化一个待添加列表list,然后余数都往后面加。求和的头都放在head中。

[二刷]:

  1. list.val = sum % 10; 可以直接往用等号list后面加点,不知道怎么来的

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

通过sum / 10的方法可以把十位取出来,不用清空,每次除10一直加就行了。倒序相加用stack

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//initialization: 2 stacks
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>(); //corner case
if (l1 == null && l2 == null) return null; //put into stacks
while (l1 != null) {
stack1.add(l1.val);
l1 = l1.next;
} while (l2 != null) {
stack2.add(l2.val);
l2 = l2.next;
} //while loop
//get sum, val, head, append the previous val to head, give head to val, reset sum
ListNode list = new ListNode(0);
int sum = 0;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
//get sum
if (!stack1.isEmpty()) sum += stack1.pop();
if (!stack2.isEmpty()) sum += stack2.pop(); //get the val node
list.val = sum % 10; ListNode head = new ListNode(sum / 10);
//append the previous val to head, head is ready
head.next = list;
//give head to val
list = head; sum /= 10;
} //return
return list.val == 0 ? list.next : list;
}
}

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

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

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

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

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

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

  7. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

  8. *445. Add Two Numbers II

    1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...

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

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

随机推荐

  1. 在IDEA中实战Git-branch

    工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...

  2. C#软件开发实例.私人订制自己的屏幕截图工具(九)使用自己定义光标,QQ截图时的光标

    版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...

  3. Django学习笔记之数据库-数据库与模型

    MySQL数据库 在网站开发中,数据库是网站的重要组成部分.只有提供数据库,数据才能够动态的展示,而不是在网页中显示一个静态的页面.数据库有很多,比如有SQL Server.Oracle.Postgr ...

  4. angular的异步处理$q的使用(promise)

    Angular中的promise: Promise是一种异步方式处理值的方法.代表了一个函数最 终可能的返回值或者抛出的异常 在之前,通常都是使用闭包或者回调来响应非同步的有意义数据 使用promis ...

  5. InnoDB存储引擎文件

    InnoDB存储引擎文件 MySQL数据库包括数据库本身的文件和存储引擎文件.数据库自身的文件由参数文件(my.cnf).错误日志文件.慢查询日志文件.查询日志文件.二进制日志文件.套接字文件.pid ...

  6. Spring生态研习【二】:SpEL(Spring Expression Language)

    1. SpEL功能简介 它是spring生态里面的一个功能强大的描述语言,支在在运行期间对象图里面的数据查询和数据操作.语法和标准的EL一样,但是支持一些额外的功能特性,最显著的就是方法调用以及基本字 ...

  7. 树莓派apt-get The value '\stable' is invalid for APT 错误

    对apt-get进行任何操作都会报错: pi@raspberrypi:~ $ sudo apt-get upgrade Reading package lists... Done E: The val ...

  8. Kong安装教程(v1.0.2)

    使用的软件 Unbuntu 虚拟机(有自己的服务器更好) PostgreSQL kong kong-dashboard docker spring boot 安装 PostgreSQL kong 需要 ...

  9. Python【每日一问】04

    问:a =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],求出列表a中所有奇数并构造新列表 答: 利用列表的元素下标遍历列表 a = [1, 2, 3, 4, 5, 6, 7, 8 ...

  10. Zookeeper 在Windows下的安装过程及测试

    安装jdk 安装Zookeeper. 在官网http://zookeeper.apache.org/下载zookeeper.我下载的是zookeeper-3.4.6版本. 解压zookeeper-3. ...