Add two numbers [LeetCode]
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
Summary: Be careful about the last carry.
ListNode * handler(int sum, int * carry, ListNode * result, ListNode * * new_head){
if(result == NULL){
result = new ListNode(sum % );
(*new_head) = result;
}else{
result->next = new ListNode(sum % );
result = result->next;
} (*carry) = sum / ;
return result;
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * new_head = NULL;
ListNode * result = NULL;
int carry = ;
while(l1 != NULL || l2 != NULL){
if(l1 == NULL){
int sum = l2->val + carry;
result = handler(sum, &carry, result, &new_head);
l2 = l2->next;
continue;
} if(l2 == NULL){
int sum = l1->val + carry;
result = handler(sum, &carry, result, &new_head);
l1 = l1->next;
continue;
} int sum = l1->val + l2->val + carry;
result = handler(sum, &carry, result, &new_head);
l1 = l1->next;
l2 = l2->next;
} if(carry != )
result->next = new ListNode(carry); return new_head;
}
Add two numbers [LeetCode]的更多相关文章
- 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 002
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] 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 ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
随机推荐
- spark的安装
官方安装地址:http://spark.apache.org/docs/latest/spark-standalone.html 准备 1.三台机器(m2.m3.m4) 2.m2位master, m3 ...
- 字节流与字符流(FileInputStream类和FileOutputStream类)
FileInputStream类和FileOutputStream类中,第一个类的源端和第二个类的目的端都是磁盘文件,它们的构造方法允许通过文件的路径名来构造相应的流.例如: FileInputSte ...
- Gulp入门教程(转载)
本人转载自: Gulp入门教程
- vs2013 类名颜色显示黑色,无法修改
vs2013 类名黑色,修改不起作用,最后找到解决办法. http://stackoverflow.com/questions/10970305/how-can-i-get-user-type-c-s ...
- s3c2440 移值u-boot-2016.03 第2篇 支持Nand flash启动
1, 要求:在4K 的代码以内,完成 NOR NAND 类型判断,初始化 NAND 复制自身到 SDRAM ,重定向. 2, 在 /arch/arm/cpu/arm920t/ 文件夹里 添加一个 in ...
- 《Unix网络编程》卷一(简介TCP/IP、基础套接字编程)
通常说函数返回某个错误值,实际上是函数返回值为-1,而全局变量errno被置为指定的常值(即称函数返回这个错误值). exit终止进程,Unix在一个进程终止时总是关闭该进程所有打开的描述符. TCP ...
- CMD执行BCP命令
C:\>BCP "EXEC GetU '2016-7-11' ,'-1'" queryout "C:\\C3Marketing\SummaryReport_test ...
- ReactJs入门教程
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...
- fork &vfork --陈皓
http://coolshell.cn/articles/7965.html http://coolshell.cn/articles/12103.html#more-12103 前两天有人问了个关于 ...
- 首师大附中科创教育平台 我的刷题记录 0325 50212228海岛帝国:LYF的太空运输站
今天给大家献上“D”级题:50212228海岛帝国:LYF的太空运输站!! 试题编号:0325 50212228海岛帝国:LYF的太空运输站 难度级别:D: 运行时间限制:40ms: 运行 ...