【LeetCode刷题系列 - 002题】Add Two Numbers
题目:
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
代码(C++实现):
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// 结果链表的首节点
ListNode *head = nullptr;
// 尾插法建立单链表指向尾节点
ListNode *tail = nullptr;
// num1为两整数相加后的个位数值
int num1 = ;
// num2为两整数相加后的进位数值
int num2 = ;
// node1Val为l1当前节点的数据域或者0
int node1Val = ;
// node2Val为l2当前节点的数据域或者0
int node2Val = ;
// count变量的设置是为了尾插法建立单链表设置的计数器
int count = ; while(l1 != nullptr || l2 != nullptr)
{
// 如果节点不为空,则取节点的数据域否则让节点的数据域为0
if(l1 == nullptr)
{
node1Val = ;
}else
{
node1Val = l1->val;
}
if(l2 == nullptr)
{
node2Val = ;
}else
{
node2Val = l2->val;
}
// 本次计算结果 = 本次计算的node1Val + 本次计算的node1Va2 + 进位值
num1 = node1Val + node2Val + num2;
if(num1 >= )
{
num1 = num1 - ;
num2 = ;
}else
{
num2 = ;
}
// 为建立结果链表创建节点
ListNode *newNode = new ListNode(num1);
// 尾插法建立结果单链表,如果是首节点,需要进行特殊处理
if(count == )
{
head = tail = newNode;
}else
{
tail->next = newNode;
tail = newNode;
}
// 链表向前移动并释放原始链表所占的内存空间
if(l1 != nullptr)
{
ListNode *tempNode1 = l1;
l1 = l1->next;
delete tempNode1;
}
if(l2 != nullptr)
{
ListNode *tempNode2 = l2;
l2 = l2->next;
delete tempNode2;
} /* 为了解决例如情况:l1 = [5]
l2 = [5]
这种情况
*/
if(l1 == nullptr && l2 == nullptr && num2 != )
{
ListNode *newNode = new ListNode(num2);
tail->next = newNode;
tail = newNode;
return head;
}
count++; } return head;
}
};
【LeetCode刷题系列 - 002题】Add Two Numbers的更多相关文章
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- C++基础知识面试精选100题系列(11-20题)[C++ basics]
[原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-11-20.html [题 ...
- C++基础知识面试精选100题系列(1-10题)[C++ basics]
[原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...
- LeetCode 2:两数相加 Add Two Numbers
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- No.002:Add Two Numbers
问题: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode 2. 两数相加(Add Two Numbers)
题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
随机推荐
- 在linux上构建gitolite
每台机器生成密钥前要设置邮箱和用户名: git config --global user.name "admin" git config --global user.email & ...
- 在做MVC和WebApi写返回数据时,可以这样定义
public class Messages { /// <summary> /// 返回包含是否成功以及消息字符结果 /// </summary> /// <param ...
- Preloading Your ASP.NET Applications
You may have noticed that the first request to an ASP.NET Web site takes longer than subsequent requ ...
- 通过jedis远程访问redis服务器
一.jedis简介 类似于mysql数据库,一般开发都需要通过代码去访问redis服务器,对于主流的开发语言,redis提供了访问的客户端接口. https://redis.io/clients 而对 ...
- Scaffold-DbContext 命令参数
NAME Scaffold-DbContext SYNOPSIS Scaffolds a DbContext and entity types for a database. SYNTAX Scaff ...
- 从内部入手,浅谈malloc和new的区别
想要理解一样事物,就要先用自己的语言去描述一件事物.在我查阅资料后,发现malloc函数简单说来就是空闲内存空间收集器,并把空闲空间关联起来,用术语来说就是:将空闲内存块合并起来并称为"闲置 ...
- FTP学习笔记
FTP有两个连接方式 1.控制连接 2.数据连接 控制链接 标准端口为21 用于数据传输中的控制 数据连接 标准端口20 用于数据传输中的上传 下载数据 数据传输的连接方式,主动连接 被动连接. ...
- Problem B: 故障电灯(light)
考虑对电灯进行差分:若第i个电灯和第i + 1个电灯状态不同,则在第i个位置上放一个球 这样我们就放置了不超过2n个球,且必然是偶数个 于是问题转化为:有m个球,每一步可以把一个球平移奇质数个位置,两 ...
- 【学习】数据聚合和分组运算【groupby】
分组键可以有多种方式,且类型不必相同 列表或数组, 某长度与待分组的轴一样 表示DataFrame某个列名的值 字典或Series,给出待分组轴上的值与分组名之间的对应关系 函数用于处理轴索引或索引中 ...
- VS Code 1.18版本更新内容整理(2017年10月 October 2017)
久前开始使用的VS Code,使用一段时间以后确实感觉比之前在用的Sublime Text好很多,可能是汉化及插件方面使用做的更好吧. 今天推送到更新到1.18,按我的个性,喜欢一个东西的话,我就回去 ...