Lintcode: Add Two Numbers
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
ListNode *addLists(ListNode *l1, ListNode *l2) {
// write your code here
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
ListNode *ptr1 = l1, *ptr2 = l2;
ListNode *result = new ListNode(-);
ListNode *pre = result;
int carry = , val = ;
while (ptr1 != NULL || ptr2 != NULL) {
int val1 = ptr1 == NULL?:ptr1->val;
int val2 = ptr2 == NULL?:ptr2->val;
int sum = val1 + val2 + carry;
val = sum;
carry = sum/;
pre->next = new ListNode(val);
pre = pre->next;
if (ptr1!=NULL)
{
ptr1 = ptr1->next;
}
if (ptr2!=NULL)
{
ptr2 = ptr2->next;
}
}
if (carry == ) {
pre->next = new ListNode();
}
return result->next;
}
};
Lintcode: Add Two Numbers的更多相关文章
- [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 两个数字相加之二
		
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 ...
 - 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 ...
 
随机推荐
- javascript 原型继承
			
因为javascript没有专门的机制去实现类,所以这里只能是借助它的函数能够嵌套的机制来模拟实现类.在javascript中,一个函数,可以包含变量,也可以包含其它的函数,那么,这样子的话,我们就可 ...
 - Odoo(OpenERP)配置文件openerp-server.conf详解
			
原文地址:http://blog.csdn.net/wangnan537/article/details/42283465 [options] ; addons模块的查找路径 addons_path ...
 - ASIHttpRequest release 包无法发出请求
			
ASIHttpRequest 在 Release 模式下,Optimize 后会导致发不出请求. 解决方案: 去掉这两个文件的 Optimization:ASIFormDataRequest.m AS ...
 - (ios7) 解决代码布局View, ios7 中 subView 高度增加StatusBar20dp的问题,保证Ios6,ios7代码一致
			
在ios7 布局中,Status Bar 和 ToolBar ,NavigateBar 等都包含在ViewControl的主View中. 这样原来ios6 的View布局 整体向上移动了20dp,下面 ...
 - android4.0 中关于内外置sd卡的获取及读写权限问题
			
from://http://blog.chinaunix.net/uid-26727976-id-3146895.html 在2.x的版本中,在manifest中配置的权限android.permis ...
 - HDU 1575 Tr A(矩阵高速幂)
			
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
 - WCF:该不该用枚举值
			
WCF支持枚举,不过在个别场景下会出现服务消费失败,如:传递或返回的枚举值(本质是int或其它)没有在枚举中定义.这种异常还很难定位,出现这种情况一般是因为BUG,因此简单的放弃使用枚举可能不是一个明 ...
 - SQL:修复脚本的几点注意事项
			
背景 系统上线之后一定会出现需求变动,某些需求变动要求会对系统数据产生影响,因此需要修复脚本,本文介绍修复脚本的几点事项. 注意事项 包含在事务中 使用事务,但是先rollback tran,在真实环 ...
 - poj 3071 Football(概率dp)
			
id=3071">http://poj.org/problem? id=3071 大致题意:有2^n个足球队分成n组打比赛.给出一个矩阵a[][],a[i][j]表示i队赢得j队的概率 ...
 - 一幅图秒懂LoadAverage(负载)
			
转自:http://www.habadog.com/2015/02/27/what-is-load-average/ 一幅图秒懂LoadAverage(负载) 一.什么是Load Average? ...