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

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

于是我决定采取另一种在网上新学到的方法:这个方法就是将链表中的数字串起来,当做一个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. 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录

    观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...

  2. go学习笔记-并发

    并发 goroutine goroutine是Go并行设计的核心.goroutine说到底其实就是协程,但是它比线程更小,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这 ...

  3. SpringBoot 解决ModelAndView强转Json问题

    最近一直在做SpringBoot升级的项目,碰到了一个很蛋疼的问题. 我们项目和前端的AngularJs通过Json来传递信息,但是我们有一块的代码在Controller返回的是ModelAndVIe ...

  4. 优步UBER司机全国各地奖励政策汇总 (2月15日-2月21日)

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

  5. 北京Uber优步司机奖励政策(11月30日~12月4日)

    用户组:人民优步(适用于12月1日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...

  6. 成都Uber优步司机奖励政策(1月15日)

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

  7. MySQL高级-性能分析Explain

    1.使用Explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的.分析你的查询语句或是表结构的性能瓶颈 . 2.执行方法:Explain + SQL语句 解释 ...

  8. 利用反射获取Model值

    public ActionResult Base(UserModel Model) { Model.Tel = string.Format("{0}-{1}", Model.Are ...

  9. 「国庆训练」Kingdom of Obsession(HDU-5943)

    题意 给定\(s,n\),把\(s+1,s+2,...,s+n\)这\(n\)个数填到\(1,2,...,n\)里,要求\(x\)只能填到\(x\)的因子的位置(即题目中\(x\%y=0\)那么x才能 ...

  10. Linux命令应用大词典-第12章 程序编译

    12.1 gcc:GNU项目的C和C++编译器 12.2 gdberver:为GNU调试的远程服务器 12.3 cmake:跨平台的Makefile生成工具 12.4 indent:更改通过插入或删除 ...