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.而我们需要做的就是将两条链表代表的 ...
随机推荐
- phpcms调用一个指定的栏目的url和栏目名称
{$CATEGORY[$catid]['url']} 指定栏目URL代码 {$CATEGORY[$catid]['catname']} 指定栏目名称代码 {$CATEGORYS[41]['url']} ...
- sybase 收集常用sql语句
-------创建sybase设备 语句--------- disk init name="DEV_DB_CLIENT_DAT26", physname="F:\syba ...
- 不会全排列算法(Javascript实现),我教你呀!
今天我很郁闷,在实验室凑合睡了一晚,准备白天大干一场,结果一整天就只做出了一道算法题.看来还是经验不足呀,同志仍需努力呀. 算法题目要求是这样的: Return the number of total ...
- 分布式缓存技术memcached学习(三)——memcached内存管理机制
几个重要概念 Slab memcached通过slab机制进行内存的分配和回收,slab是一个内存块,它是memcached一次申请内存的最小单位,.在启动memcached的时候一般会使用参数-m指 ...
- web api :Action Results in Web API 2
原文:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results Web api 返回 ...
- HDU5322 Hope(DP + CDQ分治 + NTT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5322 Description Hope is a good thing, which can ...
- AngularJs表单验证
常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要在输入字段元素上添加HTML5标记required即可: <input type="text" requir ...
- 踩坑事件:不能对基于文本的临时表使用sql insert语句
先来描述一下问题: 如果你是从基于文本的数据源来创建DataFrame的,当你将DataFrame注册为临时表后,如果对这个临时表进行insert into 操作,会抛出异常的. 问题答案参见:htt ...
- Compass的误解与新发现
最后个人感觉 Compass是 css世界的.NET 很久以前看到 Compass 误以为是css编译器,因为总是在看到如何安装Scss的文章里面看到的 知道最近不小心进入 Compass官网溜达,才 ...
- PKUSC2016
day x(x<0) 外出培训倒数第二天晚上发烧了....逃过了第二天早上的考试,orz 抢到rank 1 的commonc神犇!! day 0 下午到了北大,发了两张50元饭卡.这是第三次来北 ...