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. li之间的间隙问题

    1.间隙是有代码格式中的换行符产生,对代码进行压缩处理或手动删除换行就好:

  2. TensorFlow实战——个性化推荐

    原创文章,转载请注明出处: http://blog.csdn.net/chengcheng1394/article/details/78820529 请安装TensorFlow1.0,Python3. ...

  3. 计算机网络协议包头赏析-IP

    上次和大家聊了聊以太网的帧格式,本文会讲解IP数据报格式的定义. == 开门见山,先上图: 任何一个IP数据报都是由首部和数据两部分组成,而且首部基本是固定长度的,长度为20字节.这一点很重要,其他都 ...

  4. python java scala 单例模式

    Python class Single2(object): """ 同一个对象 """ __instance = None def __ne ...

  5. winform clickonce在线安装

    转 http://swanmsg.blog.sohu.com/162994305.html

  6. javascript 中Array.prototype.sort 函数的用法

    来源:http://www.jb51.net/article/5769.htm JavaScript中对变量的操作都是通过引用方式,而对数组也一样. 前两天想要对一个数组进行复制,一直苦于找不到办法( ...

  7. js基础-数组及数据类型

    数组也是引用类型 构造函数创建数组 Object 构造函数类型(所有类型基类)   Array 构造函数类型 求幂运算符 **   2**32-1 数组容量最大 arry.length 如果减小len ...

  8. 构建BSP (boardsupport packet)

    由于移植期间遇到较多头文件包含及常量定义问题,故在此总结如下: 常量定义一般涉及到寄存器定义.寄存器配置常量定义,寄存器配置常量一般在驱动目录下自定义,所以如果编译过程中出现常量未定义的情况一般是属于 ...

  9. 静态html返回

    在这篇文章中我们介绍后台路由的概念,后台的路由根据路径返回相应的内容, 首先我们建立一个服务器 let port = 3000 //监听端口let fs = require ('fs')//用来生成可 ...

  10. 2017-2018面向对象程序设计(Java)课程助教工作职责及分工

    2017-2018面向对象程序设计(Java)课程助教工作职责及分工 一.研究生助教团队名单 张光辉,王玉环,牟巧玲,徐进 二.工作职责与要求 1) 针对所指导学习小组学生,批改课程实验作业每周一次, ...