[LeetCode 题解]: Add Two Numbers
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
题解: (1)注意处理进位问题。 (2) 注意两个链表长度不相等的时候。
class Solution {
public:
ListNode *add(ListNode *l1, ListNode *l2,int c)
{
ListNode *head= new ListNode();
if(l1==NULL && l2==NULL)
{
if(c==)
return NULL;
else
{
head->val=;
return head;
}
}
if(l1==NULL)
return add(head,l2,c);
if(l2==NULL)
return add(l1,head,c);
if(l1!=NULL && l2!=NULL)
{
head->val =(l1->val + l2->val +c)%;
c = (l1->val + l2->val +c)/;
head->next = add(l1->next,l2->next,c);
}
return head;
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int c=;
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode *head = add(l1,l2,c);
return head;
}
};
转载请注明出处:http://www.cnblogs.com/double-win/谢谢
[LeetCode 题解]: Add Two Numbers的更多相关文章
- leetcode 题解 Add Two Numbers(两个单链表求和)
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode题解——Add Two Numbers
题目: 两个数字求和,数字用链表表示,每一个结点代表一位.链表顺序与数字顺序相反,即表头存放数字的最低位. 解法: 分别遍历两个链表的每个结点,对两个结点求和即可.要维护一个变量保存每次相加之后的进位 ...
- 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 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- 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] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- 13_java之final|static|包|匿名对象|代码块|内部类
01final关键字概念 * A: 概述 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承 ...
- Nginx配置ProxyCache缓存
利用nginx cache缓存网站数据 nginx本身就有缓存功能,能够缓存静态对象,比如图片.CSS.JS等内容直接缓存到本地,下次访问相同对象时,直接从缓存即可,无需访问后端静态服务器以及存储存储 ...
- angularjs之向下一个页面传参
原理: 1.在a标签跳转时,连接后增加一个参数值 2.在路由中接收 3.在控制器中接收 实现: 1.<a href="#/list/{{val.id}}"> 2.在js ...
- 通过CSS 给界面必选项添加星号
在制作网页的时候,如果一个选项是必填的,通常会给选项添加一个红色星号,来引起用户的注意:最近笔者刚好遇见一个类似的需求,本来可以用html标签和style就可搞定,由于笔者需要改动的界面比较多(六个) ...
- openSUSE Linux 忘记root密码的解决方法
openSUSE Linux 忘记root密码的解决方法 : 对于大部分linux发行版本,忘记root密码的时候,是可以通过单用户模式来重设密码的. 如在redhat/fedora 下,可以通过在启 ...
- Kibana安装(图文详解)(多节点的ELK集群安装在一个节点就好)
对于Kibana ,我们知道,是Elasticsearch/Logstash/Kibana的必不可少成员. 前提: Elasticsearch-2.4.3的下载(图文详解) Elasticsearch ...
- iOS学习之UIPickerView控件的简单使用
UIPickerView控件在给用户选择某些特定的数据时经常使用到,这里演示一个简单的选择数据,显示在UITextField输入框里,把UIPickerView作为输入View,用Toolbar作为选 ...
- springMVC环境搭建(1)
工作一年以来,写的都是.net,最近比较闲,想把之前学过的java相关的东西捡起来,也学点新的东西.以前做过SSH架构,一直好奇spring mvc是怎么样的,所以今天试试看. 总体的代码结构 手动输 ...
- XP下安装IIS的图文教程(无光盘)
IIS5.1安装文件包下载地址:http://yunpan.cn/QzBZGugw84wEr 安装记录: 1. 将IIS5.1安装文件包解压 2. 开始-->控制面板-->添加/删除程序- ...
- Makefile里面打印信息
Makefile的规则相对来说还是比较复杂的,上手不容易,没有系统研究过,往往搞不清楚状况.如果掌握了基本的调试手段,那对我们写出正确的Makefile会非常有帮助.而在Makefile中,最重要的调 ...