【题解】【链表】【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 ...
随机推荐
- MySQL 查看表结构
mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; use information_s ...
- 使用ContentProvider管理多媒体-----查看多媒体数据中的所有图片
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- [js]变量声明、函数声明、函数定义式、形参之间的执行顺序
一.当函数声明和函数定义式(变量赋值)同名时 function ledi(){ alert('ledi1'); }; ledi(); var ledi = function (){ alert('le ...
- IT公司100题-8-智力题
问题1: 有两个房间,一间房里有三盏灯,另一间房有控制着三盏灯的三个开关, 这两个房间是分割开的,从一间里不能看到另一间的情况. 现在要求受训者分别进这两房间一次,然后判断出这三盏灯分别是由哪个开关控 ...
- Hibernate中的一级缓存、二级缓存和懒加载
1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...
- CodeForces 546B C(Contest #1)
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- 根据IP定位获取城市代码
public String getCityID() throws IOException{ URL url = new URL("http://61.4.185.48:81/g/" ...
- Unity优化之减少Drawcall
简单来说,Drawcall就是屏幕渲染一次所需要的开销,为了较少消耗,提高性能,一般有以下几种方法. 一: 批处理 1.动态批处理 如果动态物体共用着相同的材质,那么Unity会自动对这些物体进行批处 ...
- C语言输出规定长度的整数,不够位数前面补零
今天在做ACM题目的时候,遇到了这么一个问题,还真别说,这个以前真的没用过,当时就傻掉了,还好这个世界有Google,通过搜索了解了输出这种格式的C语言实现方法.但是没有找到C++的实现方法,希望知道 ...
- [pjsip]Pjlib中的链表结构
Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: 图1:一般链表结构 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表 ...