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

题目

俩数用链表表示,做个加法

思路

1. use stack to covert input list data

2. sum up from pop item from two stacks to a desired linkedlist

代码

 class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) return null;
if (l2 == null) return l1;
if (l1 == null) return l2;
// use stack to convert list's order
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>(); while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
} while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
} int sum = 0;
ListNode head = new ListNode(0);
// sum up to a new linkedlist
while (!s1.isEmpty() || !s2.isEmpty()) {
if (!s1.isEmpty()) sum += s1.pop();
if (!s2.isEmpty()) sum += s2.pop(); int val = sum % 10; ListNode node = new ListNode(0);
head.val = val;
node.next = head;
head = node; sum /= 10;
}
if (sum == 0){
return head.next;
}
head.val = sum;
return head;
}
}

[leetcode]445. Add Two Numbers II 两数相加II的更多相关文章

  1. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

  2. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  3. 2. Add Two Numbers[M]两数相加

    题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  4. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

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

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

  6. Java实现 LeetCode 445 两数相加 II

    445. 两数相加 II 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会 ...

  7. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

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

  8. LeetCode 445——两数相加 II

    1. 题目 2. 解答 2.1 方法一 在 LeetCode 206--反转链表 和 LeetCode 2--两数相加 的基础上,先对两个链表进行反转,然后求出和后再进行反转即可. /** * Def ...

  9. Leetcode 445. 两数相加 II

    1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...

随机推荐

  1. python环境和工具

    1.版本问题 python2.X和python3.X是不兼容,所以选择如果选择了2.X版本,那么为了避免兼容性的问题,在以后使用其他python库或者工具时,也需要选择相对应的版本. 下载地址:htt ...

  2. 剑指offer例题——旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  3. RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World(转载)

    RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World 一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到 ...

  4. RabbitMQ、Memcached、SQLAlchemy

    一.RabbitMQ 1.基础概念 rabbitMQ说白了就是一个消息队列,类似于Queue,也是生产者与消费者模型.只不过做了扩展,所不同的是Queue在内存中的消息队列,而RabbitMQ是部署在 ...

  5. GitHub中国区前100名到底是什么样的人?向大佬们学习。

    本文转自:码迷 http://www.mamicode.com/info-detail-1267434.html 本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排 ...

  6. C# 图像处理:Bitmap 与 Image 之间的转换

    Image img = this.pictureBox1.Image; Bitmap map = new Bitmap(img); Image img = Bitmap; Image和Bitmap类概 ...

  7. JavaScript中的构造函数 renturn

    javascript中构造函数是不需要有返回值的,但是如果其中添加了 return 语句结果会如何呢: 看如下代码: 示例1. var Calculator = function () { retur ...

  8. vuex this.$store.state.属性和mapState的属性中的一点点区别

    做泰康公众号的项目时候有一个需求创建公众号的时候后台有一个社区id提供给后台展现人员和部门,在群发消息时候也要给后台一个社区id只不过获取社区的id接口和上一个不是一样的,本来在页面中写了两个sele ...

  9. 2018面向对象程序设计(Java)第9周学习指导及要求

    2018面向对象程序设计(Java)第9周学习指导及要求(2018.10.25-2018.10.28)   学习目标 1.掌握java异常处理技术: 2.了解断言的用法: 3.了解日志的用途: 4.掌 ...

  10. 第三章,DNA序列的进化演变

    31.前言 3.1.两个序列间的核苷酸差异 来自同一祖先序列的两条后裔序列,之间的核苷酸的差异随着时间的增加而变大.简单的计量方法,p距离 3.2.核苷酸代替数的估计 3.3.Jukes和Cantor ...