[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.而我们需要做的就是将两条链表代表的 ...
随机推荐
- 学习MVC框架的步骤
1.搭建环境 2.了解控制层和视图层的映射 3.控制层和视图层的传值 交互 4.异常处理 5.页面标签 6.文件上传 7.框架源代码
- Servlet中的配置 web.xml
url-pattern配置 可以为同一个Servlet配置多个url-pattern: <servlet> <servlet-name>DoGetPostDemo</se ...
- linux 可用内存查看
用free命令查看. 下面是一个例子(单位是MB): [root@linuxzgf ~]# free -m total used free shared buffers cachedMem: 7982 ...
- 问答精华-IntelliJ IDEA快捷键大全
这篇文章介绍了idea的默认快捷键http://www.jikexueyuan.com/blog/229.html 另外:老师将快捷键设置为eclipse的了,你需要在preference里面找到ke ...
- centos7 下安装MongoDB
查看MongoDB的最新版官方下载地址: https://www.mongodb.com/download-center#community 使用wget命令下载安装包 wget https://fa ...
- 页面设计--Grid列表
Grid列表控件 功能:主要实现Html Table功能,支持多表头.固定表头.固定列.输出.组合查询.编辑功能 优点:可以快速的通过数据集合生成多表头.多样式的列表.通过简单的拖拉实现多层表头. G ...
- java内部类的继承
1.public class OuterInnerClass extends ClassA.ClassB{ public OuterInnerClass(ClassA a) ...
- php文字水印和php图片水印实现代码
本文章向码农们介绍php文字水印和php图片水印实现代码,需要的码农可以参考一下. php 文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件. 实现 ...
- 动手学servlet(一) 第一个servlet程序
1.文件>新建>动态WEB项目 "javaeedemo">在Java Resource的src下新建包“servletdemo”,包下新建一个类“MyServet ...
- Android常用知识笔记
1. 安卓图片自适应 android从1.6和更高,Google为了方便开发者对于各种分辨率机型的移植而增加了自动适配的功能 <supports-screens android:largeS ...