题目:

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的更多相关文章

  1. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  2. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  3. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  4. C++基础知识面试精选100题系列(11-20题)[C++ basics]

    [原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-11-20.html [题 ...

  5. C++基础知识面试精选100题系列(1-10题)[C++ basics]

    [原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...

  6. LeetCode 2:两数相加 Add Two Numbers

    ​给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  7. No.002:Add Two Numbers

    问题: You are given two linked lists representing two non-negative numbers.  The digits are stored in ...

  8. LeetCode 2. 两数相加(Add Two Numbers)

    题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...

  9. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

随机推荐

  1. [UE4]快速移动,给单位向量加一个力

    一.(Vector_End- Vector_Start ).Normalize,获取从起始位置指向目标位置的单位向量. 二.给单位向量乘以一个浮点数,即给向量加一个力,是往向量方向移动 每一帧往目标点 ...

  2. iOS XML解析使用-韩国庆

    欢迎-------(北京-iOS移动开发金牌教师QQ:2592675215)韩老师给你带来XML解析课程 今天给大家讲解下xml解析的第三方简单用法:首先我解释下,json和xml解析格式. JSON ...

  3. arrayList转换为数据

    ArrayList arrayList = SetTools.loadfile(path); string[] str = (string[])arrayList.ToArray(typeof(str ...

  4. python版本的简单贪吃蛇

    先看看效果,白色的条是蛇(简单勿怪,有研究的同学请告知做的美观点),做了一个笑脸是糖果,背景弄了一个图, 代码也是从其他人那边弄来的,改了一部分直接可以在window上直接运行 代码如下: #codi ...

  5. jquery中对小数进行取整

    var uu=Math.floor(5.36) 向下取整 结果为5 var uu=Math.floor(5.88) 结果为5 Math.ceil(5.33) 向上取整,结果为6 Math.round( ...

  6. qt qextserialport __imp_SetupDiGetDeviceRegistryPropertyW

    使用 qextserialport 编写串口助手的时候,提示找不到 __imp_SetupDiGetDeviceRegistryPropertyW,经过摸索有以下两种解决方法: 第一种: 把相应的源文 ...

  7. 63.1拓展之box-shadow属性

    效果地址:https://scrimba.com/c/cQpyKbUp 效果图: HTML code: <div class="loader"></div> ...

  8. maven dependency的版本冲突问题

    在改造一个旧项目中,遇到各种问题. 旧项目有十多个模块,因为没有一个统一的父pom,它们对第三方的jar的版本没有统一. 虽然也存在公共的依赖模块,比如commons.util,但是,我们的模块中,有 ...

  9. C++builder Tokyo 调用com 不正确的变量类型

    C++builder Tokyo 调用com 不正确的变量类型 tt.OleFunction("interface_call","MS01",&erro ...

  10. 关于Qrc文件的用法

    在python文件xxx.py中调用资源文件,一般来说,需要将资源放在xxx.py的相同目录下:然而,当在xxx.py下建立一个统一目录/rec则需要建立xxx.qrc文件才能让xxx.py调用,调用 ...