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 ...
随机推荐
- Centos 6.5 升级python到版本2.7.12
查看python版本: python --version 1.下载Python-2.7.12 wget https://www.python.org/ftp/python/2.7.12/Python- ...
- sping mvc+uploadify 上传文件大小控制3部曲
页面使用uploadify 上传控件,使用spring CommonsMultipartipartResolver , 反向代理nginx nginx 配置文件 client_max_body_siz ...
- 关于DELL服务器如果采购散件,进行服务器升级的相关说明
拿DELL服务器内存来说明这个情况,其他硬盘等等是 一样的: 1.DELL服务器的内存散件,从购买之日起,质保期是一年: 2.但是如果你把内存插到能兼容这个内存的服务器上去使用,请注意我的字眼,是能兼 ...
- 操他妈的,jquery1.4以上不能用toggle()轮流切换函数
query 1.9里面已经删除了toggle(fn1, fn2)函数 (2013-05-07 13:44:27) 转载▼ 标签: it 分类: js jquery 1.9里面已经删除了toggle(f ...
- 如何干净的清除Slave同步信息
MySQL> show master status; +------------------+-----------+--------------+------------------+---- ...
- SparkStreaming 的编程模型
依赖管理 基本套路 Dstream输入源 ---input DStream Dstream输入源--- Receiver 内置的input Dstream : Basic Source 内置的inpu ...
- 2k8 32bit下载
Windows Server 2008 32-bit Standard(标准版) Windows Server 2008 32-bit Enterprise(企业版) Windows Server 2 ...
- linux系统安装pycharm环境
1.安装Pycharm环境参考网站: https://www.linuxidc.com/Linux/2018-04/152003.htm 注册:http://idea.lanyus.com/ wind ...
- Alpha版本检测报告
1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...
- Socket IO Web实时推送
1服务器pom.xml引入 <!-- 服务端 --> <dependency> <groupId>com.corundumstudio.socketio</g ...