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 ...
随机推荐
- MATLAB 不能保存变量问题及解决办法
在使用matlab保存结构体.元胞数组等等的变量时,matlab总是提示 警告: 未保存变量 'session'.对于大于 2GB 的变量,请使用 MAT 文件版本 7.3 或更高版本. 问题如下: ...
- maven groupID 和 ArtifactID的区别与作用
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...
- STRTOK函数和STRTOK_R函数
STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...
- SqlServer2005 海量数据 数据表分区解决难题
超大型数据库的大小常常达到数百GB,有时甚至要用TB来计算.而单表的数据量往往会达到上亿的记录,并且记录数会随着时间而增长.这不但影响着数据库的运行效率,也增大数据库的维护难度.除了表的数据量外,对表 ...
- aaronyang的百度地图API之LBS云 笔记[位置数据 geotable]
位置数据表 geotable DEMO下载 我们再创建一个 leverTerminal表 添加 手机价格,手机型号,手机唯一码,用户id 新建一个html页面,引入最新的jquery包,1.8.2以 ...
- FaceBook登陆API -- Login with API calls
Login with API calls Related Topics Understanding sessions FBSession Error handling FBError FBLoginC ...
- Android Viewpager加Fragment做界面切换时数据消失的解决方式
今天遇到多个Fragment切换,回来后页面空白的情况,找到这个博客方法设置了一下,就可以了 vpAdapter = new VpAdapter(getSupportFragmentManager() ...
- 记github上搭建独立域名的免费博客的方法过程
前提:拥有github帐号,linux上安装好了git. 全局路线: 1. 设计一个你想要的二级域名,并在git上创建一个以[二级域名.github.com]作为项目名的repository. 过程详 ...
- 跟我学SharePoint 2013视频培训课程——删除恢复、文档离线工作(11)
课程简介 第11天,怎样在SharePoint 2013中删除.恢复文档.文档离线工作. 视频 SharePoint 2013 交流群 41032413
- TCP网络编程杂谈
作为一名IT工程师,网络通信编程相信都会接触到,比如Web开发的HTTP库,Java中的Netty,或者C/C++中的Libevent,Libev等第三方通信库,甚至是直接使用Socket API,但 ...