【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/ 题 ...
随机推荐
- Google JavaScript样式指南
Google JavaScript样式指南 目录 1简介 1.1术语说明 1.2指南说明 2源文件基础知识 2.1文件名 2.2文件编码:UTF-8 2.3特殊字符 3源文件结构 3.1许可或版权 ...
- C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变
class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...
- Spring 框架学习—控制反转(IOC)
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建. 简单来说,Spring是一个分层的JavaSE/EEfull-st ...
- windows 程序员电脑设置
程序员电脑设置: 1.详细目录 a.在一个文件夹下设为详细信息 b.win7点击"组织"-->"文件夹的搜索选项"-->"查看" ...
- leveldb源码分析--WriteBatch
从[leveldb源码分析--插入删除流程]和WriteBatch其名我们就很轻易的知道,这个是leveldb内部的一个批量写的结构,在leveldb为了提高插入和删除的效率,在其插入过程中都采用了批 ...
- 提示"Zend Optimizer not installed"卸载安装也不行,什么原因如何解决?
如题:Zend Optimizer not installed可能原因及解决方法 Optimizer, Zend 在配置php服务器的时候,所有的东西都安装好了,就是浏览一个要求zend的程序的时候, ...
- Tomcat性能监控之Probe
目前采用java进行开发的系统居多,这些系统运行在java容器中,通过对容器的监控可以了解到java进程的运行状况,分析java程序问题.目前市面上流行的中间件有很多(Tomcat.jetty.jbo ...
- swift关于UIView设置frame值的extension
swift关于UIView设置frame值的extension 使用 说明 1. 使用如上图,很简单,不再赘述 2. 在extension给添加的计算属性提供getter,setter方法即可 源码 ...
- (matlab)plot画图的颜色线型(转)
http://wenku.baidu.com/link?url=SVVMVH8QlDIu2hVKDtoBYs6l0CnQvFnFHJJ9yexmYVKQqhz47qIr7aK7LOf8nN0qNdy8 ...
- css3实现border渐变色
案例1 .box{ width: 100px; height: 100px; border:10px solid #ddd; border-image: -webkit-linear-gradient ...