Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

https://oj.leetcode.com/problems/add-two-numbers/

解答:

比较简单,直接用2个指针往后移动,并且将2个list的值相加,并且计算上carry值。注意,在while循环中加上carry == 1的判断,这样可以自动在链尾加上carry 的值。

 package Algorithms.list;

 import Algorithms.algorithm.others.ListNode;

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class AddTwoNumbers {
public static void main(String[] str) {
ListNode n1 = new ListNode(9); ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(9);
ListNode l3 = new ListNode(9);
ListNode l4 = new ListNode(9);
ListNode l5 = new ListNode(9);
ListNode l6 = new ListNode(9);
ListNode l7 = new ListNode(9);
ListNode l8 = new ListNode(9);
ListNode l9 = new ListNode(9);
ListNode l10 = new ListNode(9); l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6;
l6.next = l7;
l7.next = l8;
l8.next = l9;
l9.next = l10; System.out.println(addTwoNumbers(n1, l1).toString());
} public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
return null;
} ListNode dummy = new ListNode(0);
ListNode tail = dummy;
int carry = 0; while (l1 != null || l2 != null || carry == 1) {
int sum = carry;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
} if (l2 != null) {
sum += l2.val;
l2 = l2.next;
} carry = sum / 10; // create a new node and add it to the tail.
ListNode cur = new ListNode(sum % 10);
tail.next = cur;
tail = tail.next;
} return dummy.next;
}
}

代码:

AddTwoNumbers

LeetCode: Add Two Numbers 解题报告的更多相关文章

  1. LeetCode 2. Add Two Numbers 解题报告

    题意: 有两个链表,它们表示逆序的两个非负数.例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出.如342+465 = 807,你需要把结果表达为(7 -&g ...

  2. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  3. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  4. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. 注意Hibernate4在开发当中的一些改变(转)

    注意Hibernate4在开发当中的一些改变 原文:http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861 Hibernate4的改动较 ...

  2. 树莓派进阶之路 (031) -字符问题(1) - GBK汉字编码表(转)

    转载:http://blog.sina.com.cn/s/blog_8184e033010109ug.html   基本简介 GB码,全称是GB2312-80<信息交换用汉字编码字符集基本集&g ...

  3. react.js 测试

    <html>    <head>        <title>hellow</title>        <script src="ht ...

  4. 【JQuery】事件冒泡及使用jQuery阻止

    (1)什么是事件起泡 首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什么要传播呢 ...

  5. php截取字符去掉最后一个字符

    $str="中国.美国.俄罗斯.德国."$str=substr($str,0,-1); 输出结果为:中国.美国.俄罗斯.德国

  6. 利用eclipse+jdk1.8搭建Java开发环境(超具体的)

    利用eclipse+jdk1.8搭建Java开发环境 转载请声明出处:http://blog.csdn.net/u013067166/article/details/50267003 引言:eclip ...

  7. FreeSWITCH取消Digest校验流程

    方法一.通过使用acl配置 <list name="domains" default="deny"> <!-- domain= is spec ...

  8. How to convert String to Date – Java

    In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners ar ...

  9. java实现windows下amr转换为mp3(可实现微信语音和qq语音转换)

    最近做一个项目需要将微信的语音文件放在页面进行播放,查了好多资料发现,web页面直接播放并没有一个好的解决方案,于是就想到了先将amr文件转换成易于在页面播放的mp3文件,然后在进行播放,现在将amr ...

  10. Windows平台下tomcat+java的web程序持续占cpu问题调试

    1.问题 Tomcat服务器跑了一段时间后,发现Tomcat进程占用的CPU资源在80%-100%间,加上其它的进程,整个服务器的CPU处理100%运行状态. 2.通过process explorer ...