【题解】【链表】【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
思路:乍一看以为是大整数,实则相当简单,比Add Binary还要简单
代码:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
return addTwoDigit(l1,l2,);
}
ListNode *addTwoDigit(ListNode *d1, ListNode *d2, int carry){
int sum = carry;
ListNode *n1 = d1, *n2 = d2;//不要轻易修改输入参数, 尤其是链表问题中
if(d1 != NULL){
sum += d1->val;
n1 = d1->next;
}
if(d2 != NULL){
sum += d2->val;
n2 = d2->next;
}
ListNode *newNode = new ListNode(sum%); if(d1 == NULL && d2 == NULL){
if(sum == )
return NULL;//Avoid test case:{0}, {0} => {0,0}
return newNode;
} ListNode *nextNode = addTwoDigit(n1, n2, sum/);
newNode->next = nextNode;
return newNode;
}
【题解】【链表】【Leetcode】Add Two Numbers的更多相关文章
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- excel快捷键设置
Excel技能 按键 结果 序号 Alt+1 合并后居中 1 ALT+2 边框 2 ALT+3 边框线型 3 ALT+4 格式刷 4 ALT+5 清除格式 5 CTRL+SHIFT+P 设置字体 6 ...
- 注册并启动 Reporting Services SharePoint 服务
在安装 SharePoint 之前已安装 Reporting Services SharePoint 模式.所以Reporting Services SharePoint 是不能正常使用的. 安装完S ...
- K需要修改的内容
1.需要保存默认案件,所有相关的页面的Title都要显示默认按键信息. 2.播放器需要调整,左侧的是播放信息,用户选择:案件/设备/然后就把该目录下的文件都展示出来.用户选择的时候马上进行播放.右侧有 ...
- "数学口袋精灵"bug的发现
团队成员的博客园地址: 曾治业:http://www.cnblogs.com/zzy999/ 陈焕恳:http://www.cnblogs.com/4249ken/ 蓝叶:http://www.cnb ...
- javascript——拖拽(完整兼容代码)
拖拽,是JS经常会用到的效果,在网上有很多的这样那样的拖拽效果,但其中往往大多有各种各养的问题,功能不全,无法兼容,而且修改的时候 也是十分麻烦. 其实拖拽的原理很简单,无非是鼠标的三个动作的解析,以 ...
- Android窃取用户信息新思路
0×01 我们能得到哪些android手机上的app敏感信息手机上的app敏感信息◦通讯录,通讯记录,短信◦各种app的帐号密码,输入信息资料等◦各种影音资料,照片资料◦等等0×02 我们有哪些方法 ...
- linux内核启动笔记
一. 1.解压 tar xjf linux-2.6.22.6.tar.bz2 2.打补丁 patch -p1 < ../linux-2.6.22.6_jz2440.patch 3.配置 ...
- C++-bool的值
/////////////////////////////////////////////////////////////////////////////// // // FileName : boo ...
- 戴文的Linux内核专题:08内核配置(5)
转自Linux中国 Linux内核拥有许多可以配置的特性,接下来我们还有许多要配置. 下一个可以配置的特性是x86的随机数生成器(x86 architectural random number gen ...
- android shape详解
shape--> shape属性: rectangle: 矩形,默认的形状,可以画出直角矩形.圆角矩形.弧形等 solid: 设置形状填充的颜色,只有android:color一个属性 andr ...