[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 ...
随机推荐
- Vim配置:在win10下用vim编译运行C/C++(异步插件管理,一键运行)
为什么用Vim 重新调配vim,追求尽量简单些. 安装 官网下载 PC: MS-DOS and MS-Windows下的 For modern MS-Windows systems (starting ...
- java 控制台 输入字符串
import java.util.Scanner; //导入输入类 public static void main(String[] args) { //创建输入对象 Scanner s ...
- Oracle11gr2_ADG管理之switchover补充
之前演示的switchver总是提示下面的错误,并且需要人工干预: Oracle11gr2_ADG管理之switchover实战 DGMGRL> switchover to snewtest; ...
- 【转】内存耗用:VSS/RSS/PSS/USS
Terms VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS- Prop ...
- JAVA用email.jar发送邮件
1 jar包 email.jar包,网上下载 2 源代码 package zjr.amy.emil.test; import java.util.Date; import java.util.Prop ...
- 一道面试题Lintcode196-Find the Missing Number
http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...
- 【HDU5862】Counting Intersections
题意 有n条线段,且都平行于坐标轴.对于每条线段,给出两个端点的坐标.问一共有多少个线段的交点. 分析 最最简单的扫描法了.用线段树或者树状数组都可以. 由题目可知,线段只有两种,要么平行于x轴要么平 ...
- jmeter-plugins-dubbo & DevToolBox
jmeter-plugins-dubbo使用 A. 下载jmeter并安装,http://jmeter.apache.org/download_jmeter.cgi(文中使用的版本是3.3,理论上高版 ...
- Mask_rcnn openpose realsense
cd /home/luo/Desktop/MyFile/Mask_RCNN_Openpose_Realsense python realsense_mask_openpose_2019032601.p ...
- ubuntu eclipse opencv环境配置
项目——Properties——C/C++ Build——Settings 配置包含目录: GCC C++ Compiler ——Includes /usr/include /usr/local/ ...