LeetCode: Add Two Numbers 解题报告
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;
}
}
代码:
LeetCode: Add Two Numbers 解题报告的更多相关文章
- LeetCode 2. Add Two Numbers 解题报告
题意: 有两个链表,它们表示逆序的两个非负数.例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出.如342+465 = 807,你需要把结果表达为(7 -&g ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- java struts2入门学习实例--使用struts进行验证
一.为什么要进行验证? 验证几乎是注册登录的必须前提,验证的主要作用有两点: 1.安全性 2.对用户提供差异化服务. 二.如何验证? ActionSupport类中有一个validate()方法,这是 ...
- 信号量 Linux函数 semget();semctl();semop();(转)
本文出自:http://blog.csdn.net/ta893115871/article/details/7505560 Linux进程通信之信号量 信号量(semaphore)是变量,是一种特殊的 ...
- HDU 4585 Shaolin (STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 【Algorithm】基数排序
一. 算法描述 基数排序(以整形为例),将整形10进制按每位拆分,然后从低位到高位依次比较各个位.主要分为三个过程: 分配,先从个位开始,根据位值(0-9)分别放到0~9号桶中(比如53,个位为3,则 ...
- Lighttpd1.4.20源代码分析 笔记 状态机之错误处理和连接关闭
这里所说的错误有两种: 1.http协议规定的错误,如404错误. 2.server执行过程中的错误.如write错误. 对于http协议规定的错误,这里的"错误"是针对clien ...
- nodejs的Express框架源码分析、工作流程分析
nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...
- (面试)Statement和PrepareStatement有什么区别
(1)Statement用于执行静态sql语句,在执行时,必须指定一个事先准备好的sql语句.PrepareStatement是预编译的sql语句对象,sql语句被预编译并保存在对象中.被封装的sql ...
- [转] spring的普通类中如何取session和request对像
在使用spring时,经常需要在普通类中获取session,request等对像.比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2 ...
- ubuntu14安装redis
1.下载源文件 wget http://download.redis.io/releases/redis-3.0.7.tar.gz 2.解压 tar vxzf redis-3.0.7.tar.gz 3 ...
- idea没有代码自动提示功能和包自动引入不了问题
idea没有代码自动提示功能和包自动引入不了问题 原因:节电模式 File -> Power Save Mode (被勾选了) 处理方法: File -> Power Save Mode ...