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.而我们需要做的就是将两条链表代表的 ...
随机推荐
- TFS二次开发系列:五、工作项查询
本节将讲述如何查询工作项,用于二次开发中定义获取工作项列表. 使用WorkItemStore.Query方法进行查询工作项,其使用的语法和SQL语法类似: Select [标题] from worki ...
- 跨域调用webapi
web端跨域调用webapi 在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Wordpress 所有hoor列表
d 在插件加载的时候执行 wp_footer 加载页面底部时执行 admin_menu 加载管理员菜单时执行 wp_head 在body标签的开始添加html内容 after_setup_theme ...
- 一个叫vtysh的命令行shell
目前的工作就是在这个叫vtysh的工具上进行修改,终极目的是把它作为一个代替shell的东西.以此来屏蔽用户和系统之间的接触,减少用户对系统的操作权限.
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- MySQL性能分析及explain的使用
MySQL性能分析及explain用法的知识 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selectty ...
- scrollview 嵌套 折叠效果
------------------------------- --@ CreateDate: 2015.08.05 --@ Author: 王成成 --@ FileName: BaoSh ...
- Unity学习疑问记录之保卫伦敦塔学习体会
1.生成的prefab如果要产生反向: Instantiate(Rocket, rocketPosition.position, Quaternion.Euler(new Vector3(0,0,18 ...