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 ...
随机推荐
- 移植Python2到TQ2440
环境 Python:2.7.13 开发板: TQ2440 工具链: arm-none-linux-gnueabi-gcc 4.8.3 概述 前面已经把Python3移植到TQ2440上面的,现在我们移 ...
- ASP.NET Web API中通过ETag实现缓存
通常情况下Server是无状态的,在ASP.NET Web API中,我们可以让服务端响应体中产生ETag属性,起到缓存的作用.大致实现原理是: 1.服务端的响应体中返回一个ETag属性2.客户端通过 ...
- Android:客户端和服务器之间传输数据加密
Android客户端与服务器进行数据传输时,一般会涉及到两类数据的加密情况,一类是只有创建者才能知道的数据,比如密码:另一类是其他比较重要的,但是可以逆向解密的数据. 第一类:密码类的数据,为了让用户 ...
- [Android Security] 反编译常用工具
copy : https://down.52pojie.cn/Tools/Disassemblers/
- IPython介绍
本文编写时,IPython最新的版本为6.3和5.4. 介绍 IPython 是 Fernando 在 2001 开始开发的一个交互式的Python解释执行环境.众所周知,Python提供了一个交互执 ...
- 【UOJ Round #5】
构造+贪心/数论 为什么只有两个标题呢……因为第二题我不会…… 怎样提高智商 构造题……然而一开始半天我都yy不出来…… 后来我想:这题应该不会特别麻烦,而且既然样例只给了1,可能再给大一点就让人发现 ...
- go语言之进阶篇字符串操作常用函数介绍
下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档. 一.字符串操作常用函数介绍 1.Contains func Contains(s, substr st ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- 算法 数组中出现次数最多的数字 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 用Visual C#来清空回收站(1)
视窗操作系统的回收站是对文件一种保护措施,他主要是作用是不言而喻的.在新的视窗2000系统之中,当我把文件删除到回收站中以后,按动"清空回收站"按钮,想清空回收站,此时往往提示&q ...