[leetcode]_Add Two Numbers
题目:两个链表存储数字,然后求和,和值存储在一个链表中。
代码:
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode result = head;
int carry = 0 , tempSum = 0;
while(l1 != null || l2 != null){
int v1 , v2;
v1 = (l1 != null) ? l1.val : 0;
v2 = (l2 != null) ? l2.val : 0;
tempSum = v1 + v2 + carry;
carry = tempSum / 10;
tempSum = tempSum % 10;
head.next = new ListNode(tempSum);
head = head.next;
l1 = (l1 != null) ? l1.next : null;
l2 = (l2 != null) ? l2.next : null;
}
if(carry > 0) head.next = new ListNode(carry);
return result.next;
}
[leetcode]_Add Two Numbers的更多相关文章
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [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 II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
随机推荐
- 卸载oracle
1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. 2. 开始->程序->Oracle - OraHome81 ...
- 编写一个go gRPC的服务
前置条件: 获取 gRPC-go 源码 $ go get google.golang.org/grpc 简单例子的源码位置: $ cd $GOPATH/src/google.golang.org/gr ...
- Redis链接上不的问题
问题描述: 同样配置的redis及系统环境,在两台服务器(A.B两台服务)上部署,但是其中一台(A),运行一段时间,就链接不上了,从开始运行redis到redis链接不上,这个时间间隔,不一定有时候是 ...
- asp.net子窗体与父窗体交互
今天在项目上遇到了这个问题,其实只是window.returnValue的简单应用,不是asp.net的专属内容.作为积累,记录一个简单的实现模型. 图1 用到的文件 从图1中我们可以看到,只用到了 ...
- 项目积累——js应用
//解决由前台向后台传值中文乱码的问题 encodeURI($("#xmjhbgFile").val())//前台JS中数据加码 String fjmc = java.net.UR ...
- 稀疏矩阵乘法加法等的java实现
原创声明:本文系作者原创,转载请写明出处. 一.前言 前几天由于科研需要,一直在搞矩阵的稀疏表示的乘法,不过最近虽然把程序写出来了,还是无法处理大规模的矩阵(虽然已经是稀疏了).原因可能是 ...
- Fiddler抓包工具的使用
下载 自行去官网下载 http://www.telerik.com/fiddler 配置Fiddler 1.打开Fiddler, Tools-> Fiddler Options -> HT ...
- OpenGL: 渲染管线理论
http://blog.csdn.net/augusdi/article/details/19934463 学习着色器,并理解着色器的工作机制,就要对OpenGL的固定功能管线有深入的了解. 首先要知 ...
- 【Unity Shaders】学习笔记——SurfaceShader(五)让纹理动起来
[Unity Shaders]学习笔记——SurfaceShader(五)让纹理动起来 转载请注明出处:http://www.cnblogs.com/-867259206/p/5611222.html ...
- MXNet符号编程
构成符号: 符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 下图定义了符号变量A, 符号变量B, 生成了符号变量C, 其中, A, B为参数节点, C为内部节点! mxnet ...