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 ...
随机推荐
- Visual Studio 2012 trial version
Update: vs2012.5.iso http://download.microsoft.com/download/9/F/1/9F1DEA0F-97CC-4CC4-9B4D-0DB45B8261 ...
- Linux安装MySql.Data for mono
wget http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-6.8.3-noinstall.zipunzip m ...
- IP地址
if (!/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/.test($.trim($('#add ...
- openstack vm_lifecycle
nova instance状态:power_state, vm_state, task_state 2015-09-22 Openstack 185 nova instance有3种状态:power_ ...
- C++中输入输出的重定向
重定向,就是: 把 原来的 cin 从键盘输入 改为从文件输入. 把 原来的 cout 向屏幕输出 改为输出到文件. 例如: cin>>line; 原来要从键盘拍入.现在自动到某文件读取, ...
- Kolmogorov 的数学观与业绩
https://www.douban.com/group/topic/11395706/ 作者:伊藤清 当我得知苏联伟大的数学家,84岁的 Andreyii Nikolaevich Kolmogoro ...
- WPF的二维绘图(一)——DrawingContext
DrawingContext比较类似WinForm中的Graphics 类,是基础的绘图对象,用于绘制各种图形,它主要API有如下几种: 绘图API 绘图API一般形为DrawingXXX系列,常用的 ...
- java类型占用字节数&类型转换
1.整型类型 存储需求 bit数 取值范围 备注int 4字节 4*8 short ...
- CSS中浏览器开发商特定的CSS属性
浏览器制造商(像Microsoft.Mozilla等,还有WebKit的后台人员等)通常会为他们的浏览器增加新的功能来测试新的特性, 或者实现一直在考虑但还没有得到标准组织批准的CSS扩展.在这些情况 ...
- Java中的自增问题(i=i++)
也许我这是在较真, 但是我们确实有时候就不小心就错写为这种情况了. 看如下代码: public class Test{ public static void main(String[] args){ ...