[leetCode][016] 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
【题意】:
首先题意理解,输入为两个单链表,每一个链表的元素均为0~9的数字,而每一个链表示一个数字,头部是最低位,尾部是最高位。例如上述题目中Input的两个链表分别表示342 以及 465.
那么问题就来了,输出为新的链表,该链表以同样的方式表示输入的两个数字之和。
【Key Point】:
记录进位
【坑】:
可以写两个方法,将链表内容转换成数字,以及将数字转换成对应的链表。于是就有了以下步骤:
- list1, list2 <convertListToNum> num1, num2;
- value = num1 + num2;
- value <convertNumToList> newList;
如果题目中规定了链表的长度,这种方法未尝不可,可是没有~ 计算机中无论int(最大表示到2^4),long(最大也表示到2^4),long long(最大表示到2^8)等都有极限,而链表理论上可以形成很大的数字,很容易就能够突破 , 所以使用数值类型来存储结果是不可能通过所有case的。因此可以使用非数值型来存储,但是无形中增加了该题目的复杂度。
【解答】:
既然题目给的链表结果就是从低位向高位(很舒服,如果逆向又得费点事),逐次针对链表两个元素进行相加,记录每一次的进位,注意 题目并没有说输入的两个链表长度一致。以下是代码实现,通过leetCode的OJ
class Solution {
public:
// 记录每一次的进位,这样就突破了数值型的限制
ListNode *addTwoNumbers2(ListNode *l1, ListNode *l2){
ListNode *root = NULL, *pre = NULL;
int nCarry = ; // 每一个数位能够产生的进位最多为1,所以可以采用bool或者1bit表示
while (NULL != l1 || NULL != l2){
int nodeVal = ;
if (NULL == l1){
nodeVal = l2->val + nCarry;
l2 = l2->next;
}
else if (NULL == l2){
nodeVal = l1->val + nCarry;
l1 = l1->next;
}
else{
nodeVal = l1->val + l2->val + nCarry;
l1 = l1->next;
l2 = l2->next;
}
if (nodeVal >= ){ // 产生进位
nCarry = ;
nodeVal %= ;
}
else{
nCarry = ; // 进位清零
}
ListNode *node = new ListNode(nodeVal);
if (pre == NULL){
root = node;
}
else{
pre->next = node;
}
pre = node; // 记录上次节点,串联整个链表使用
}// while
if (nCarry != ){ // 当链表结束如果还有进位,需要增加一个最高位节点
ListNode * lastNode = new ListNode(nCarry);
if (NULL != pre){
pre->next = lastNode;
}
}// nCarry != 0
return root;
}
}
【leetCode Submission】

【运行结果】:

如果有什么问题,希望各位不吝赐教,小弟感恩答谢!
[leetCode][016] 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 面试: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 Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
随机推荐
- Linux 面试题总结
一. 填空题1. 在Linux系统中,以 (文件) 方式访问设备 .2. Linux内核引导时,从文件 (/etc/fstab) 中读取要加载的文件系统.3. Linux文件系统中每个文件用 (索引节 ...
- 在CentOS 6.4中编译安装gcc 4.8.1
在CentOS 6.4中编译安装gcc 4.8.1 分类: C/C++ Linux/Unix2013-11-28 21:02 1877人阅读 评论(0) 收藏 举报 原文链接:http://www.c ...
- 《linux备份与恢复之一》.tar.bz2与.tar.gz格式的文本压缩率比较
对于文本压缩,据说bzip的算法要优于gzip,从而拥有更好的压缩比.特地找了两个文件来做一下测试,以下为测试结果: (1)源文件为591MB, .tar.bz2文件为61MB(10.32%), ...
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API
本文截取自情缘 1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集 ...
- object-c 系列教程
1.object-c 基本数据类型 2.object-c 控制语句 3.object-c面向对象1 4.object-c面向对象2 5.object-c 继承多态 动态数据类型
- 如何提高cocos2d-x-spine骨骼动画加载速度
下面分2点来说: 1. 时间消耗点:io和现场解析 解决方案:加载过的骨骼动画就不要每次重新加载,不要每次都去加载json文件和atlas,我推荐使用 static CCSkeletonAnimati ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 17.Python笔记之memcached&redis
作者:刘耀 博客:www.liuyao.me 博客园:www.cnblogs.com/liu-yao 一.Memcached 1.介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动 ...
- docke跨主机通信之gre隧道
GRE简介 GRE可以对网络层的任何协议来进行封装,类似LVS的IPIP协议,在原有的数据报上增加GRE协议数据报.然后在网络上传输,到达对端后,解开GRE数据报头,得到真实的数据报.其中的mac地址 ...
- Controller与View之间的数据传递
1)Controller向View传递数据ViewData["message"] = "Hello";//使用ViewData传递数据ViewBag.Time ...