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 ...
随机推荐
- php7 数据库操作的 方法
连接数据库的方法PHP7.0以上的: 方法一: <?php/* Connect to a MySQL server 连接数据库服务器 */$link = mysqli_connect('loca ...
- Bootstrap:目录
ylbtech-Bootstrap:目录 1.返回顶部 1. https://getbootstrap.com/ 2. 2.返回顶部 1. http://www.runoob.com/bootstra ...
- git修改用户名和邮箱
用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. 1.查看用户名和地址 git config user.name git config us ...
- windows下面安装easy_install和pip教程
方便安装whl:安装完成后,可以使用pip install xxx.whl 安装一个python轮子 python扩展库的路径:Python\Python36\Lib\site-packages\ ...
- Angular4之常用指令
Angular4指令 NgIf <div *ngIf="false"></div> <!-- never displayed --> <d ...
- 详解Tomcat配置及使用
2018年06月27日 23:42:34 尘埃丶落定 阅读数:2351 版权声明:本文为博主原创文章,转载请附上作者与出处. https://blog.csdn.net/longyin0528/ ...
- nginx, supervisor
Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...
- 哈希学习(2)—— Hashing图像检索资源
CVPR14 图像检索papers——图像检索 1. Triangulation embedding and democratic aggregation for imagesearch (Oral ...
- 33. Linux安装配置JDK-7
安装说明 系统环境:centos-6.3安装方式:rpm安装 软件:jdk-7-linux-x64.rpm下载地址:http://www.oracle.com/technetwork/java/jav ...
- ajax 实现跨域
ajax本身是不可以跨域的,通过产生一个script标签来实现跨域.因为script标签的src属性是没有跨域的限制的. 其实设置了dataType: 'jsonp'后,$.ajax方法就和ajax ...