Algorithm——Add Two Numbers(补上周)
一、question
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: ( -> -> ) + ( -> -> )
Output: -> ->
Explanation: + = .
二、solution
(cpp)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode preHead(), *p = &preHead;
int carry=;
while(l1||l2||carry){
int sum = (l1?l1->val:)+(l2?l2->val:)+carry;
carry = sum/;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1?l1->next:;
l2 = l2?l2->next:;
}
return preHead.next;
}
};
(js)
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/ var addTwoNumbers = function(l1, l2) {
var add = 0
, ans
, head; while(l1 || l2) {
var a = l1 ? l1.val : 0
, b = l2 ? l2.val : 0; var sum = a + b + add;
add = ~~(sum / 10); var node = new ListNode(sum % 10); if (!ans)
ans = head = node;
else {
head.next = node;
head = node;
} if (l1)
l1 = l1.next;
if (l2)
l2 = l2.next;
} if (add) {
var node = new ListNode(add);
head.next = node;
head = node;
} return ans;
};
Algorithm——Add Two Numbers(补上周)的更多相关文章
- [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 ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- [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 ...
- [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; ...
随机推荐
- jQuery基础笔记(3)
day55 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-8-0 操作标签 样式操作 样式类 addClass();// 添 ...
- Storm集群参数调整
Supervisor 参数调整 修改${STORM_HOME}conf/storm.yaml文件内容 supervisor变更参数 slots 配置: 若storm host仅仅执行superviso ...
- 从负数开始 ,跟随别大人脚步 ---java
刚刚毕业 音乐生 目前在做 数据库测试和实施的相关工作 . 1个月前认识了别大人 , 打算边工作 ,边学习java 开启学习之路 . ..340多个G的java视频感觉解压完1T 足够我喝 ...
- Python小白学习之路(十七)—【内置函数二】
序列操作类函数 all() 功能:判断可迭代对象的每个元素是否都为True值注意:If the iterable is empty, return True.(举例3) 回顾:None '' ...
- 微信小程序web-view(webview) 嵌套H5页面 唤起微信支付的实现方案
场景:小程序页面有一个web-view组件,组件嵌套的H5页面,要唤起微信支付. 先讲一下我的项目,首先我是自己开发的一个H5触屏版的商城系统,里面含有购物车,订单支付等功能.然后刚开始,我们公众号里 ...
- Windows10下简单搭建zookeeper
转载请注明源出处:http://www.cnblogs.com/lighten/p/6798669.html 1 简介 zookeeper是Apache的一个开源项目,致力于开发和维护一个开源的服务器 ...
- Linq基础知识小记二
书写Linq查询有两种方法,第一种是通过方法语法(也就是扩展方法),第二种是查询表达式语法. 1.方法语法 方法语法就是通过扩展方法和Lambda表达式来创建查询 (1).链式查询 这种查询方式很多语 ...
- 解决waveInOpen录音编译x64程序出错的问题
1.之前也碰到过x86程序升级为x64程序,关键点是类型大小的使用. 之前同事碰到过一个用int表示指针的程序,程序改为x64会出错,找原因找了半天. 2.今天我也碰到了,使用aveInOpen录音, ...
- HDFS DATANODE 磁盘容量的最小值
HDFS的DATANODE的剩余空间具体要到多大?关于这个问题,下面记录下对这个问题的调查 昨天,讨论群里面给出了一个异常: op@odbtest bin]$ hadoop fs -put ../tm ...
- spring boot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服务器 ...