LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/
题目:
You are given two linked lists representing two non-negative numbers. 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
题解:
可看成是Add Two Numbers与Reverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.
Time Complexity: O(n).
Space: O(1).
AC Java:
/**
* 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) {
l1 = reverse(l1);
l2 = reverse(l2); ListNode dummy = new ListNode(0);
ListNode cur = dummy;
int carry = 0; while(l1 != null || l2!= null){
if(l1 != null){
carry += l1.val;
l1 = l1.next;
} if(l2 != null){
carry += l2.val;
l2 = l2.next;
} cur.next = new ListNode(carry%10);
carry /= 10;
cur = cur.next;
} if(carry != 0){
cur.next = new ListNode(carry);
} ListNode head = dummy.next;
dummy.next = null;
return reverse(head);
} private ListNode reverse(ListNode head){
if(head == null || head.next == null){
return head;
} ListNode tail = head;
ListNode cur = head;
ListNode pre;
ListNode temp;
while(tail.next != null){
pre = cur;
cur = tail.next;
temp = cur.next;
cur.next = pre;
tail.next = temp;
} return cur;
}
}
也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.
Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).
Space: O(n). stack用了O(n). result list用了O(n).
AC Java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
} Stack<Integer> stk1 = new Stack<Integer>();
Stack<Integer> stk2 = new Stack<Integer>();
while(l1 != null){
stk1.push(l1.val);
l1 = l1.next;
}
while(l2 != null){
stk2.push(l2.val);
l2 = l2.next;
} ListNode dummy = new ListNode(0);
int carry = 0;
while(!stk1.isEmpty() || !stk2.isEmpty()){
if(!stk1.isEmpty()){
carry += stk1.pop();
}
if(!stk2.isEmpty()){
carry += stk2.pop();
}
ListNode cur = new ListNode(carry%10);
cur.next = dummy.next;
dummy.next = cur;
carry /= 10;
}
if(carry != 0){
ListNode cur = new ListNode(1);
cur.next = dummy.next;
dummy.next = cur;
}
return dummy.next;
}
}
LeetCode Add Two Numbers II的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
随机推荐
- virtaulbox视图模式常用切换
virtaulbox发现菜单不见,如何切换视图模式呢? 很简单,通过快捷键主机(host)键 Host+F 换到全屏模 ...
- 解决win7下PIL无法打开图片的问题
找到PIL安装文件里的ImageShow.py 把第99行的 return "start /wait %s && del /f %s" % (file, file) ...
- OneThink开发框架
OneThink是一个开源的内容管理框架,基于最新的ThinkPHP3.2版本开发,提供更方便.更安全的WEB应用开发体验,采用了全新的架构设计和命名空间机制,融合了模块化.驱动化和插件化的设计理念于 ...
- Android开发常用工具类
来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...
- (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题
我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...
- zorka源码解读之Instrument实现原理
主要用到三方面技术: beanshell来实现可扩展:告诉zorkaAgent插桩的具体需求,包括插桩的方法和值.插桩的时机.插桩追踪记录方式等. Instrument来通过代理的方式访问JVM,实现 ...
- bzoj4196 [Noi2015]软件包管理器 树链剖分+线段树
先把树剖分了(又是dfs1.dfs2),然后区间求和.区间覆盖即可 难得的1A好(shui)题 ——写了那么多题,终于有一道是1A的了,加上上一次连续交了几遍A的程序,我的状态莫名好看啊233 总结: ...
- [html] 有利于seo优化的div+css命名规范
搜索引擎优化(seo)对命名规范有很多要求,下面是我收集的一些当下主流的命名(还是比较常用的): CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或 ...
- 更换app开发者账号
在开源中国上面有一个答案,http://www.oschina.net/question/2307266_237220 下面是我的执行步骤 首先在iTunes Connect中找到要更换开发者账号的a ...
- saltstsck执行sls配置
salt '*' state.sls init.pkgsalt '*' state.sls init.pkg test=true