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的更多相关文章

  1. leetcode 题解 Add Two Numbers(两个单链表求和)

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  2. LeetCode题解——Add Two Numbers

    题目: 两个数字求和,数字用链表表示,每一个结点代表一位.链表顺序与数字顺序相反,即表头存放数字的最低位. 解法: 分别遍历两个链表的每个结点,对两个结点求和即可.要维护一个变量保存每次相加之后的进位 ...

  3. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  5. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  8. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

随机推荐

  1. django之部署

    布署 从uwsgi.nginx.静态文件三个方面处理 服务器介绍 服务器:私有服务器.公有服务器 私有服务器:公司自己购买.自己维护,只布署自己的应用,可供公司内部或外网访问 公有服务器:集成好运营环 ...

  2. pycharm git工具与coding.net结合

    前提:coding.net中的项目是私密项目 问题描述:在使用pycharm自带的git工具clone(或者push)代码时出现 错误如下:Push failed: Failed with error ...

  3. winform中读取App.config中数据连接字符串

    1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...

  4. 将本地Jar包安装到maven仓库中去

    开发中会遇到无法通过pom.xml下载jar包的情况,遇到这种情况我们可以手动在本地安装jar包到本地仓库中去,这样就可以下次再用到的话不用再次联网下载,具体以oracle的驱动包ojdbc6.jar ...

  5. tomcat的中的Apache的apr是个啥东东???

    这东西的作用说白了就是让我们的项目在其他系统中也可以正常运行!!!!

  6. fdsf

    https://blog.csdn.net/chen_2890/article/details/83757022Elasticsearch环境搭建和介绍(Windows) https://blog.c ...

  7. Apache Flume的介绍安装及简单案例

    概述 Flume 是 一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的软件.Flume 的核心是把数据从数据源(source)收集过来,再将收集到的数据送到指定的目的地(sink).为了保证 ...

  8. node-mysql中防止SQL注入

    备注: 本文针对mysqljs/mysql. 为了防止SQL注入,可以将SQL中传入参数进行编码,而不是直接进行字符串拼接.在node-mysql中,防止SQL注入的常用方法有以下四种: 方法一:使用 ...

  9. 【HDU1542】Atlantis

    题意 给出n个矩形的左下角和右上角的坐标,计算总的面积(相交部分只算一次). 分析 线段树扫描线的模板题. 将每个矩形都拆成上下两条线段,然后从下网上扫,当遇到底边时就加上这个区间,遇到顶边时,就减去 ...

  10. Gym101128G:Game of Cards

    题意: 有P摞纸牌和一个数字k,每次可以从一摞中拿0-k张牌,拿完再剩下的牌中的第一张数字是几,就必须再拿几张,谁不能拿谁输. emmm感觉好像就是裸的SG游戏啊,数据不大,递推出每一摞牌的SG值,然 ...