【LeetCode445】 Add Two Numbers II★★
题目描述:

解题思路:
给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和)。
如(7->2->4->3)(7243) + (5->6->4)(564) = (7->8->0->7)(7807)
此题与【LeetCode2】Add Two Numbers解决思路类似,不同之处在于此题的数字以正序存储,故需要借助栈。
Java代码:
import java.util.Stack;
//public class LeetCode445为测试代码
public class LeetCode445 {
public static void main(String[] args) {
ListNode l1=new ListNode(7),l11=new ListNode(2),l12=new ListNode(4),l13=new ListNode(3);
l1.next=l11;
l11.next=l12;
l12.next=l13;
System.out.print("Input:["+l1.val+","+l11.val+","+l12.val+","+l13.val+"]");
ListNode l2=new ListNode(5),l21=new ListNode(6),l22=new ListNode(4);
l2.next=l21;
l21.next=l22;
System.out.println(",["+l2.val+","+l21.val+","+l22.val+"]");
ListNode list=new Solution().addTwoNumbers(l1, l2);
if(list!=null)
System.out.print("output:["+list.val);
while(list.next!=null){
System.out.print(","+list.next.val);
list.next=list.next.next;
}
System.out.println("]");
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
while(l1!=null){
stack1.push(l1.val);
l1=l1.next;
}
while(l2!=null){
stack2.push(l2.val);
l2=l2.next;
}
int carry=0;
ListNode resultList=new ListNode(0);
while(!stack1.empty()||!stack2.empty()){
int sum=carry;
if(!stack1.empty()) sum+=stack1.pop();
if(!stack2.empty()) sum+=stack2.pop();
resultList.val=sum%10;
ListNode newHead=new ListNode(sum/10);
newHead.next=resultList;
resultList=newHead;
carry=sum/10;
}
return resultList.val==0?resultList.next:resultList;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
程序结果:

【LeetCode445】 Add Two Numbers II★★的更多相关文章
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- 【LeetCode2】Add Two Numbers★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- 浏览器根对象navigator之对象属性概览
第1章 connection[试验] navigator.connection 是只读的,提供一个NetworkInformation 对象来获取设备的网络连接信息.例如用户设备的当前带宽或连接是否被 ...
- git报错:Pull is not possible because you have unmerged files解决方法
在git pull的过程中,如果有冲突,那么除了冲突的文件之外,其它的文件都会做为staged区的文件保存起来. 重现: $ git pull A Applications/Commerce/B ...
- Session、Cookie简单理解
Session: session是一种记录客户状态的机制,session是保存在服务器上的,当浏览器访问服务器的时候,服务器把客户端信息以某种形式记录在服务器上,这就是所谓的session,当浏览器再 ...
- log4j.properties 配置示例
需要的jar如下: !-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId> ...
- leetCode题解之寻找string中最后一个word的长度
1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 ‘a’,'akkk'. 2.问题分析 从后向前扫描,如果string是以空格‘ ’结尾的,就不用计数, ...
- mac os maverick下安装nginx+php-fpm via homebrew
自己虽然平时爱折腾,却很少有记下来的习惯,除非碰到特别多问题的部署才会有冲动.今天看同事折腾git,在旁边看着他mac上的evernote满满的记了好几篇,全是技术相关的记录,忽然很感慨.过去解决了很 ...
- Oracle EBS 启动调试日志
SELECT * FROM dba_source t WHERE t.TEXT LIKE '%PO_PDOI_TAX_CALCULATION_ERR%' FND:启用调试日志 FND:调试日志级别 ...
- asp.net --- Menu控件\CSS 和样式
几乎 Menu 控件外观的各个方面都可以使用 Menu 控件的属性或级联样式表 (CSS) 来管理.通过了解哪些属性控制呈现的哪些方面,可以定制菜单的外观.本主题介绍由 Menu 控件公开的样式类型, ...
- 使用innodb_ruby探查Innodb索引结构
使用innodb_ruby探查Innodb索引结构 innodb_ruby 是使用 Ruby 编写的 InnoDB 文件格式解析器.innodb_ruby 的目的是暴露一些其他隐藏的 InnoDB 原 ...
- zendstudio 默认网页打开your project的时候不显示本地主机localhost的解决方法
修改wamp配置文件c:\\wamp64\wampmanager.conf 修改为如下选项即可 默认这个选项是off关闭的,打开即可. urlAddLocalhost = "on" ...