本题题意是指将两个数倒序存储在链表中,再将两数之和同样存储在链表中输出。

我最开始的思路是将每一位相加,再考虑是否进位,但这时就需要考虑一些情况,比较麻烦。

于是我决定采取另一种在网上新学到的方法:这个方法就是将链表中的数字串起来,当做一个long,例如2->4->5,可以根据题目具体要求转化成long型的542,再做后续的操作,就很容易了。

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
long ListToLong(ListNode* l)
{
long res = ;
long tem = ;
while(l)
{
res = res + l->val * tem;
tem = tem * ;
l = l->next;
}
return res;
}
long get_n(long num)
{
long x = ;
long n = ;
while(num >= x)
{
n ++;
x = x * ;
}
return n;
}
void add(ListNode* l1, ListNode* l2)
{
while(l1->next)
{
l1 = l1->next;
}
l1->next = l2;
}
ListNode* LongToList(long num)
{
ListNode* res = new ListNode(-);
int n = get_n(num);
//cout<<"n = "<<n<<endl;
int x = ;
for(int i = ;i < n;i ++)
{
x = num % ;
num = num / ;
ListNode* node = new ListNode(x);
add(res,node);
}
return res->next;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
long num1 = ListToLong(l1);
long num2 = ListToLong(l2);
//cout<<"num1 = "<<num1<<" num2 = "<<num2<<endl;
long num3 = num1 + num2;
//cout<<"num3 = "<<num3<<endl;
ListNode* res = LongToList(num3);
return res;
}
};

但上面的代码提交后的结果很让我无语。。。见下图

只有两个示例没有通过。。。将long改成long long依旧不能通过,应该是特意添加的这两个示例,这种算法看来只能做到这步了。

于是我不得不回到最开始的思路,下面是AC代码

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
int M = ;//表示进位
ListNode* res = new ListNode();//结果链表
int flag = ;
void add(ListNode* l1, ListNode* l2)
{
while(l1->next)
{
l1 = l1->next;
}
l1->next = l2;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode* temp = new ListNode();
if(l1 == NULL && l2 == NULL)
return res->next;
else if(l1 == NULL)
{
temp->val = temp->val + M + l2->val;
if(temp->val >= )
{
temp->val = temp->val - ;
M = ;
}
else
{
M = ;
}
ListNode* e1 = new ListNode(temp->val);
add(res,e1);
if(M && (l2->next == NULL))
{
ListNode* e3 = new ListNode();
add(res,e3);
}
addTwoNumbers(NULL,l2->next);
return res->next;
}
else if(l2 == NULL)
{
temp->val = temp->val + M + l1->val;
if(temp->val >= )
{
temp->val = temp->val - ;
M = ;
}
else
{
M = ;
}
ListNode* e2 = new ListNode(temp->val);
add(res,e2);
if(M && (l1->next == NULL))
{
ListNode* e4 = new ListNode();
add(res,e4);
}
addTwoNumbers(l1->next,l2);
return res->next;
}
else
{
int x = l1->val + l2->val + M;
if(x >= )
{
x = x - ;
M = ;
}
else
{
M = ;
}
ListNode* Node = new ListNode(x);
add(res,Node);
if(l1->next == NULL && l2->next == NULL && M == )
{
ListNode* e = new ListNode();
add(res,e);
}
else
{
addTwoNumbers(l1->next,l2->next);
}
return res->next;
}
}
};

[Leetcode] 2.Add Two Numbers(List To Long,模拟)的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

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

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  10. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. C语言实例解析精粹学习笔记——19

    实例19:判断正整数n的d进制表示形式是否是回文数(顺着看和倒着看相同的数). 主要思路: 一种方法:将正整数n数转换成d进制的数,逐个比较首尾对应数字,判断是否为回文数. 另一种方法:将正整数n数转 ...

  2. PTA基础编程题目集7-2然后是几点

    有时候人们用四位数字表示一个时间,比如1106表示11点零6分.现在,你的程序要根据起始时间和流逝的时间计算出终止时间. 读入两个数字,第一个数字以这样的四位数字表示当前时间,第二个数字表示分钟数,计 ...

  3. Go Web 问题集-持续更新

    前端: 导入静态js,css报错,在确保js和css语法编写正确的前提下 GET   错误:          等问题 1.在服务器中运行:静态服务文件路径设置错误 2.本地运行:相对路径设置错误 3 ...

  4. 天津Uber优步司机奖励政策(1月11日~1月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. VINS(九)Ceres Solver优化(未完待续)

    使用Ceres Solver库处理后端优化问题,首先系统的优化函数为

  6. WPF Style Setter use a TemplateBinding?

    <Style TargetType="{x:Type local:ImageButton}"> <Setter Property="Horizontal ...

  7. 强制删除无用old windows文件夹命令

    磁盘上有旧系统留下的目录比如old.windows.program files.users(中文目录是用户,删除命令里还是要用user才有效),因为目录的特殊设置,导致无法直接删除,需要修改属性和权限 ...

  8. 了解和分析iOS Crash

    WeTest 导读 北京时间凌晨一点,苹果一年一度的发布会如期而至.新机型的发布又会让适配相关的同学忙上一阵子啦,并且iOS Crash的问题始终伴随着移动开发者.本文将从三个阶段,由浅入深的介绍如何 ...

  9. postman使用感言

    这段时间接口测试一直使用的postman,一款谷歌接口测试插件,感受如下 优点: 1.对于中小型公司来说应该是够用的,特别是一键接口环境切换,一键设置header,作为一般的接口测试来说已经很不错了, ...

  10. 关于@media不生效的问题和meta总结

    1:之前做的是两套页面.现在改成响应式布局.发现加上 @media only screen and (max-width: 500px) {    .gridmenu {        width:1 ...