Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
/**
* 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 headNode(-);
ListNode *preNode = &headNode; int carry = ;
while(L1 != NULL || L2 != NULL)
{
const int value1 = (L1 == NULL) ? : L1->val;
const int value2 = (L2 == NULL) ? : L2->val;
L1 = (L1 == NULL) ? NULL : L1->next;
L2 = (L2 == NULL) ? NULL : L2->next; const int value = (value1 + value2 + carry) % ;
carry = (value1 + value2 + carry) / ;
preNode->next = new ListNode(value);
preNode = preNode->next;
}
if(carry == )
{
preNode->next = new ListNode(carry);
preNode = preNode->next;
}
return headNode.next;
}
};
Add Two Numbers ---- LeetCode 002的更多相关文章
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add two numbers [LeetCode]
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [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 ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
随机推荐
- python 第三方模块 转 https://github.com/masterpy/zwpy_lst
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...
- sqlserver定時備份
通常备份的方式有两种:手动备份和自动备份,如果数据库服务器中有这么多的数据库需要备份,那么手动备份必定是一个很大的工作量,这个时候我们可以采用另外一种备份方式---定时自动备份. SQLServer的 ...
- Linux配置邮箱发送(MUTT/MSMTPQ)
配置邮箱发送 http://www.ilanni.com/?p=10589
- (19)odoo中的javascript
-----------更新日期15:17 2016-02-16 星期二-----------* 用到的js库 我们可以打开 addons/web/views/webclient_template. ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Ajax请求中带有IPv6地址后的百分号的问题
IPv6地址后的百分号: 对于连入网络但没有IPv6路由器或DHCPv6服务器的IPv6客户端,它们始终使用fe80::/64链路本地网络地址.如果运行Windows的计算机中有多个网络适配器连接到不 ...
- linux安装.run
chmod +x ./framework-3.6.0-linux-full.runsudo ./framework-3.6.0-linux-full.run
- 在express3.0上使用模板
express3.0取消了layout设置,为了能使用模版,经过百度后发现有个express-partials模块可以使用 1:安装 npm install express-partials 模块安装 ...
- BZOJ4007 [JLOI2015]战争调度
根本想不出来... 原来还是暴力出奇迹啊QAQ 无限ymymym中 /************************************************************** Pr ...
- Storm(1) - Setting Up Development Environment
Setting up your development environment 1. download j2se 6 SDK from http://www.oracle.com/technetwor ...