[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 ...
随机推荐
- Android屏幕录制
自己实现了Android的屏幕录制App. 用了MediaProjection类来作为源,MediaRecoder来捕捉,编码转换为本地视频. 效果图: 主要是这段代码开始录像: startActiv ...
- winform 实现局部更新(如ajax实现)而整个界面不产生闪烁的解决方案
转自原文winform 实现局部更新(如ajax实现)而整个界面不产生闪烁的解决方案 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET ...
- Django admin 使用多个数据库
admin是django自带的一个app,那它涉及的是对Model的所有对象进行增删改查,如果model来自多个数据库如何处理呢? 重写admin.ModelAdmin的如下几个方法就好了: clas ...
- 【源码阅读】Java集合之一 - ArrayList源码深度解读
Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,从ArrayList开始第一篇. ---@pdai JDK版本 ...
- T-SQL 之 执行顺序
1.sql查询语句的处理步骤,代码清单 --查询组合字段 (5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<se ...
- LUA 环境
LUA中环境是指一个函数执行的表,即一个函数在什么表中执行. 这里的函数是特殊的,是loadfile("x.lua")的返回值. loadfile("x.lua" ...
- f.lux Ubuntu 下进行安装
这几天在搞Ubuntu 看的是我眼睛特痛,于是查了一下,有Linux 版的f.lux 于是我就行了一番的安装. 步骤 命令行 1. sudo add-apt-r ...
- Python的索引迭代
Python中,迭代永远是取出元素本身,而非元素的索引. 对于有序集合,元素确实是有索引的.有的时候,我们确实想在 for 循环中拿到索引,怎么办? 方法是使用 enumerate() 函数: > ...
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException/com.atomikos.datasource.ResourceException异常解决
tomcat+mysql部署,每天早晨第一次访问web项目,出现mysql的连接timeout异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ...
- sqlserver里常用的语法
bb 为nvarchar(50)CAST(bb AS int) select MAX(CAST(bb AS int)) from AAA