[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.而我们需要做的就是将两条链表代表的 ...
随机推荐
- 九度OJ1061
//C++ sort函数的多重排序 #include <iostream> #include<algorithm> #include<string> using n ...
- Eclipse Android HH and theme
一. 汉化方法: 1.Eclipse版本查询:安装目录readme,查版本号;参照查代号如下表: 代号 平台版本 项目 主要版本发行日期 SR1发行日期 SR2发行日期 N/A 3.0 [1] N/A ...
- Eclipse 常用设置
1. eclipse中的汉字横着显示怎么解决 同一种字体有两种显示方式,比如Fixedsys Excelsior 3.01和@Fixedsys Excelsior 3.01,前一种汉字是竖着显示,后一 ...
- Air Raid(最小路径覆盖)
Description Consider a town where all the streets are one-way and each street leads from one interse ...
- Android系统下的动态Dex加载
1 问题在Android系统中,一个App的所有代码都在一个Dex文件里面.Dex是一个类似Jar的存储了多有Java编译字节码的归档文件.因为Android系统使用Dalvik虚拟机,所以需要把使用 ...
- 【原】dnsmasq小工具
1.介绍 DNSmasq是一个轻巧的,容易使用的DNS服务工具,它可以应用在内部网和Internet连接的时候的IP地址NAT转换,也可以用做小型网络的DNS服务. 它可以提供如下几个实用的功能: 1 ...
- 黑马程序员_Java基本数据的自动拆装箱及享元设计模式视频学习笔记
------- android培训.java培训.期待与您交流! ---------- 装箱:把基本数据类型装成java类(被托管?). 拆箱:把java类拆成基本数据类型(取消托管? ...
- ASP.NET MVC开发微信(二)
- 【Base64&UrlEncode】
base641.包含A-Z a-z 0-9 和加号“+”,斜杠“/” 用来作为开始的64个数字. 等号“=”用来作为后缀用途.2.2进制的.3.要比源数据多33%.4.常用于邮件.5. = 号的个数 ...
- 【caffe-windows】 caffe-master 之 cifar10 超详细
本教程尽量详细,大多步骤都有图,如果运行出错,请先对照自己的文件是否和图上的一样,包括标点啊,空格啊,斜杠,反斜杠啊之类的小细节. 本例程是在 win10 64位 caffe-master ...