题目:

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. symfony 踩坑之旅 视频实操从第九章开始

    1.annotation定义路由 @Route("/**",defaults={"name":"world"},requirements={ ...

  2. lucene搜索之高级查询

    使用Query子类查询 MatchAllDocsQuery TermQuery NumericRangeQuery BooleanQuery 使用QueryParser QueryParser Mul ...

  3. 浅谈角色换装功能--Unity简单例子实现

    在前置篇中,基本上梳理了一下换装功能背后涉及到的美术工作流.但程序员嘛,功能终归是要落到代码上的.本文中会结合Unity提供的API及之前提到的内容来实现一个简单的换装功能.效果如下: (图1:最终效 ...

  4. Python 内置函数math,random

    内置函数的一些操作 - math(数学模块) - random(随机模块) - 使用内置函数时注意需要导入 math - (ceil)向上取整,返回取整数 # 向上取整,返回向上取整的数 import ...

  5. JavaScript数组方法--concat、push

    利用了两天的时间,使用typescript和原生js重构了一下JavaScript中数组对象的主要方法,可以移步github查看. 这里,按照MDN上的文档顺序,再重新学习一下数组方法吧. conca ...

  6. LinuxMint 下 B站 番 blv 缓存 转 mp4

    参考https://www.littleqiu.net/archives/886 (不过我使用绝对路径,ffmpeg报错,相对路径没问题) 一.安装ffmpge sudo apt-get instal ...

  7. fedora 26 安装 mplayer smplayer

    dnf install mplayer dnf install smplayer 提示仓库 里没有这个软件 fedora默认不提供一些有版权争议的软件,非开源的项目包,所以需要用rpm fusion源 ...

  8. oracle入坑日记<二>认识oracle(含sqlplus基础使用)

    1.SID(数据库实例) 1.1. oracle安装的时候有一项叫[全局数据库名]的填写项,这个就是oracle的SID也是数据库的唯一标识符: 1.2.一个oracle数据库有且只有一个SID(一般 ...

  9. hash加密

    hash import hashlib content = 'its so coll'.encode('utf8') o = hashlib.sha1() # 创建一个hash对象 o.update( ...

  10. vmware 进入虚拟机VMware的系统后鼠标不能点

    vmware 进入虚拟机VMware的系统后鼠标不能点 1)关闭虚拟机,重启win10,再打开虚拟机好了 2)