【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. ...
随机推荐
- altera DDR2 IP核之仿真
在生成的IP核文件夹下,有一个testbench文件夹,里面包含了一个example测试激励和DDR2仿真模型. 如下 20 -rw-r--r-- 1 Administrator 197121 171 ...
- 使用 ping++做支付的流程
获取支付凭据 /// <summary> /// 获取支付凭据 /// </summary> /// <param name="model">& ...
- Python全栈开发记录_第八篇(模块收尾工作 json & pickle & shelve & xml)
由于上一篇篇幅较大,留下的这一点内容就想在这里说一下,顺便有个小练习给大家一起玩玩,首先来学习json 和 pickle. 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过, ...
- 使用plot_importance绘制特征重要性曲线
代码如下所示: # -*- coding: utf-8 -*- #导入需要的包 import matplotlib.pyplot as plt from sklearn import datasets ...
- [持续交付实践] pipeline使用:Shared Libraries
前言 随着pipeline交付流水线在团队中的推广,使用pipeline脚本的job也迅速增加.虽然我们已经基于公司的技术栈特点做了一个尽可能通用的pipeline脚本样例,让搭建者只需要修改几个赋值 ...
- Ubuntu离线安装docker
1.先安装依赖libltdl7_2.4.6-0.1_amd64.deb 下载链接http://archive.ubuntu.com/ubuntu/pool/main/libt/libtool/libl ...
- html--form表单
<!-- form 标签 作用:收集并提交用户的信息 属性: id 表单的id,用于js获取表单 name 表单的名字,用于js获取表单 action 表单提交的地址 method 表单提交 ...
- Flask对数据库的操作-----
首先得做好做基本的框架 # -*- encoding: utf-8 -*- from flask import Flask,render_template #导入第三方连接库sql点金术 from f ...
- 开窗函数over
select id,sum(je) over() as je from dt
- REST架构简析(原论文整理)
0 引言 目前,互联网在社会中扮演的角色越来越重要.通过互联网为广大群众提供服务,也是互联网成功的关键.互联网服务架构目前大多数都是基于REST架构来完成的.REST从它诞生至今,可以说 ...