题目描述:

解题思路:

  给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和)。

  如(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★★的更多相关文章

  1. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  2. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  6. 【Leetcode】【Medium】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  8. 【LeetCode2】Add Two Numbers★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...

  9. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. jquery实现复选框全选,全不选,反选中的问题

    今天试了一下用jquery选择复选框,本来以为很简单的东西却有bug,于是搜索了一下找到了解决方法. html代码如下(这里没有用任何样式,就没有再放css了): <html> <h ...

  2. opencv图像处理基础 (《OpenCV编程入门--毛星云》学习笔记一---五章)

    #include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgu ...

  3. 安装部署OpenPAI + VSCode 提交

    ========================================================== 安装openpai请参考这篇 https://www.cnblogs.com/ji ...

  4. Pig安装

    环境: hadoop-2.4.1.jdk1.6.0_45.pig-0.12.1   1.下载pig并解压 tar -xzvf pig-0.12.1.tar.gz 2.设置环境变量 export PIG ...

  5. Nginx 配置多站点vhost

    假设你想在Linux Nginx中用不同的域名访问不同的目录,这时就要配置多个vhost,具体配置如下,假设网站根目录设定在/var/www/ 1.在/var/www/下新建两个目录 /var/www ...

  6. [C/C++]如何解读返回函数指针的函数声明

    今天在看<深入理解C++11>的时候,看到一段有意思的代码: int (*(*pf())())() { return nullptr; } 我立刻就懵了——从来没有见过这样的函数声明.那么 ...

  7. ActionContext和ServletActionContext小结(转)

    ActionContext和ServletActionContext小结 1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Act ...

  8. C# 实现水印

    直接上源码 public class WaterTextBox : TextBox { //private const int EM_SETCUEBANNER = 0x1501; //[DllImpo ...

  9. ELT探索之旅2 kettle配置

    java环境变量配置: path增加    ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 双击spoon.bat即可

  10. November 16th 2016 Week 47th Wednesday

    Success is falling nine times and getting up ten. 成功就是哪怕跌倒九次,也要在第十次爬起来. For most of us, we may choos ...