2 Add Two Numbers
// Java
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return add(l1, l2, false);
}
private ListNode add(ListNode l1, ListNode l2, boolean step) {
if (l1 == null && l2 == null && !step)
return null;
int i = l1 != null ? l1.val : 0;
int j = l2 != null ? l2.val : 0;
ListNode result;
if (step) {
result = new ListNode((i + j + 1) % 10);
step = i + j + 1 >= 10;
} else {
result = new ListNode((i + j) % 10);
step = i + j >= 10;
}
result.next = add(l1 == null ? null : l1.next,
l2 == null ? null : l2.next, step);
return result;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
2 Add Two Numbers的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
随机推荐
- java 静态方法和实例方法的区别(转)
静态方法和实例方法的区别主要体现在两个方面: 在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这 ...
- ASP.net 中手工调用WS(POST方式)
ASP.net 中手工调用WS(POST方式)核心代码:string strUrl="http://localhost:21695/service1.asmx/getmythmod" ...
- QT学习(打个广告)
最近全面学习QT与c++,希望同行能够指教,于是打算建个群QQ群:85439482,欢迎大家,本群主要专注于QT皮肤库积累,软件架构以及标准c++学习.
- reference local jar & customize manifest
dependencies { compile files('libs/ghost4j-0.5.1.jar') compile files('libs/jai_imageio.jar') compile ...
- RabbitMQ消息机制单人分发
static void Main(string[] args) { // More.SendMessage(); var factory = new ConnectionFactory(); fact ...
- 重新介绍 JavaScript
简介 为什么需要这个重新介绍呢?因为 JavaScript 已经完全可以被称为世界上被误解最严重的编程语言了.虽然它被当做玩具来用,但是藏在让人迷惑的简单表象下面的,是强大的语言特性.从2005年,一 ...
- Java堆内存的十个要点
Java中的堆空间是什么? 当Java程序开始运行时,JVM会从操作系统获取一些内存.JVM使用这些内存,这些内存的一部分就是堆内存.堆内存通常在存储地址的底层,向上排列.当一个对象通过new关键字或 ...
- .NET Framework 的 Quirk Version
今天在CSDN上看到一个帖子 :".net 4.0和4.5不同版本的Uri.ToString行为不同?", 调试.NET Framework 源代码发现,是这句代码起的作用 int ...
- AngularJS 深入理解 $scope
$scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达式执行的上下文.有了$scope 就在视图和控制器之间建立了一个通道,基于作用域视图在修改数据时会立刻更 ...
- JAVA-系统-【2】-创建自增长的用户表
[2]创建数据库表 用户表 自增 1.用户表结构 数据excel 表1 2.创建表 Create table A_USER( id number primary key, username ) n ...