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. Python函数部分

    Python函数的初识 Python函数的进阶 Python中的闭包与迭代器 Python生成器/推导式/生成器表达式 Python内置函数二 (递归函数,匿名函数,二分法)

  2. (halcon) derivate_vector_field

    derivate_vector_field: Convolve a vector field with derivatives of the Gaussian 用高斯导数卷积向量场 derivate_ ...

  3. 当一个项目中同时存在webroot和webcontext时

    当一个项目中同时存在webroot和webcontext时,注意一定要删除那些没在使用的.还有要发布其中一个想要的目录到服务器中,具体方法是  选择相应工程-----properties-----de ...

  4. 第2章地址Address(WCF全面解析3)

    WCF顾明思义,就是在Windows平台下解决通信(C,Communication)的基础框架(F,Foundation)问题. 终结点是WCF最为核心的对象,因为它承载了所有通信功能.服务通过相应的 ...

  5. T-SQL 重复读(Double Read)问题的理解

    我的理解是: step1,假设表里有100行有序记录, 事务1从row 1 开始读取到了row 50 并准备继续读取完这100行. 要注意的是,sql server 会自动释放已经读取了的row的锁. ...

  6. 【FZU2178】礼物分配

    题意 在双胞胎兄弟Eric与R.W的生日会上,他们共收到了N个礼物,生日过后他们决定分配这N个礼物(numv+numw=N).对于每个礼物他们俩有着各自心中的价值vi和wi,他们要求各自分到的礼物数目 ...

  7. 为什么要用Android Studio?

    为什么要用Android Studio 本书节选自<Android Studio实用指南> 作者: 毕小朋 目前本书已上传到百度阅读,在百度中搜索[Anroid Studio实用指南]便可 ...

  8. 利用django中间件CsrfViewMiddleware防止csrf攻击

    一.在django后台处理 1.将django的setting中的加入django.contrib.messages.middleware.MessageMiddleware,一般新建的django项 ...

  9. CentOS搭建VSFTP服务器

    一.安装vsftpd 1.查看是否已经安装vsftpd 2.如果没有,就安装 3.测试是否安装成功 4.安装成功设置开机启动 二.配置vsftpd 1.修改配置文件/etc/vsftpd/vsftpd ...

  10. unary_function和binary_function详解

    1.unary_function和binary_function介绍 1.1 unary_function介绍 unary_function可以作为一个一元函数对象的基类,它只定义了参数和返回值的类型 ...