2.Add Two Numbers (List)
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) {
int sum = l1->val + l2->val;
int carry = sum/;
ListNode* root = new ListNode(sum%);
ListNode* current = root; l1 = l1->next;
l2 = l2->next;
while(l1 && l2){
sum = l1->val + l2->val + carry;
carry = sum/;
current->next = new ListNode(sum%);
current = current->next;
l1 = l1->next;
l2 = l2->next;
} while(l1){
sum = l1->val+ carry;
carry = sum/;
current->next = new ListNode(sum%);
current = current->next;
l1 = l1->next;
} while(l2){
sum = l2->val+ carry;
carry = sum/;
current->next = new ListNode(sum%);
current = current->next;
l2 = l2->next;
} if(carry){
current->next = new ListNode(carry);
}
return root;
}
};
2.Add Two Numbers (List)的更多相关文章
- [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 ...
随机推荐
- 学习笔记之XML
什么是QName - Benjieming_Wang的专栏 - CSDN博客 http://blog.csdn.net/Benjieming_Wang/article/details/5959961 ...
- ssh-keygen的使用方法(无密码访问)
一.概述 1.就是为了让两个linux机器之间使用ssh不需要用户名和密码.采用了数字签名RSA或者DSA来完成这个操作 2.模型分析 假设 A (192.168.20.59)为客户机器,B(192. ...
- mdm9x07 ATC AT+QCFG usbnet
1 中文AT命令详解 1.1. AT+QCFG 扩展配置 AT+ QCFG 扩展配置 测试命令 AT+QCFG=? 响应 …… +QCFG: "usbnet" ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- Redis如何存储对象与集合示例详解
前言 大家都知道在项目中,缓存以及mq消息队列可以说是不可或缺的2个重要技术.前者主要是为了减轻数据库压力,大幅度提升性能.后者主要是为了提高用户的体验度,我理解的是再后端做的一个ajax请求(异 ...
- 发送短信验证码及调用短信接口与C# 后台 post 发送
#region 调用短信接口 public ActionResult Mobile(string Tel)//调用接口 { Random rm = new Random(); int i; strin ...
- 20180129周一之学习PYTHON笔记【安装、查看工作目录、】
一,安装过程中多选一个ADD的项,免去设置环境变量. 二,PYAUTOGUI模块控制键鼠. IMAGE模块. ----------------------python 如何查看与更换工作目录----- ...
- 全文检索在 MySQL
中就是一个 FULLTEXT 类型索引.FULLTEXT 索引用于 MyISAM 表,可以在 CREATE TABLE 时或之后使用 ALTER TABLE 或 CREATE INDEX 在 CHAR ...
- 53. sql2005“备份集中的数据库备份与现有的xx数据库不同”解决方法
RESTORE DATABASE LiveBOS_KeJiFROM DISK = 'D:\LiveBOS_KeJi_backup_201503090000.bak' --bak文件路径with rep ...
- NLP—WordNet——词与词之间的最小距离
WordNet,是由Princeton 大学的心理学家,语言学家和计算机工程师联合设计的一种基于认知语言学的英语词典.它不是光把单词以字母顺序排列,而且按照单词的意义组成一个“单词的网络”.我们这次的 ...