题目:

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]Event Tick

    一.每一帧都会触发Event Tick事件,“Delta Seconds”参数表示当前帧说花费的时间 二.因为各种原因(比如卡帧等),每一帧所花费的时间并不是相同的

  2. mybatis逆向工程没有报错,但是也没有pojo和Mapper文件问题

    如果你使用的逆向工程是自己手写上去的配置文件,那么错误的范围就太大了.如果是你导入以前使用过的逆向工程,那么没有生成文件很可能是使用的操作系统不同. 原因:逆向工程中的路径问题,windows和mac ...

  3. linux Ubuntu 16.04安装 postgresql

    两次的重装系统安装数据库折磨的死去活来. 安装步骤: 1,运行  sudo apt-get update     --这个用来查找数据源 2 ,运行  apt install postgresql  ...

  4. 关于对CSS中超链接那部分的设置

    a:link{                                  //正常下的超链接 color:red;                          //超链接的颜色 text ...

  5. ubuntu安装python版本的opencv

    安装命令: pip install opencv-python

  6. 63.1拓展之box-shadow属性

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

  7. kettle删除移动文件

  8. python大法好——Python XML解析

    Python XML解析 什么是XML? XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这些部件加以标识. 它也是元标记语言,即定义了用于定义其他与 ...

  9. dojo下的dom按钮与dijit/form/Button

    众所周知,在dojo里存在dom和widget两个类型,dom指的是普通类型的HTML元素,包括各种类型的标签.按钮.输入框等等,而widget指的是dojo自身所带的模板,同时也包括按钮.输入框等等 ...

  10. 12Linux_Apache_vsftpd(匿名开发模式)

    网站:让我们的用户可以通过浏览器去访问到的文档的资源. windows:IIS Linux:Apache Nginx(吃得少,干的多) APACHE:基金会,公司,软件 httpd:软件名称,软件包名 ...