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

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

于是我决定采取另一种在网上新学到的方法:这个方法就是将链表中的数字串起来,当做一个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. silverlight 图形报表开发

    前端: <UserControl x:Class="SLThree.CharReport" xmlns="http://schemas.microsoft.com/ ...

  2. Hibernate怎么用

    一.为什么用Hibernate? [核心:对象关系映射] Hibernate是对jdbc的轻量级封装,可以简化数据库连接操作, 在该框架之前,数据库的操作步骤是: 1.根据连接字串获取连接 2.执行s ...

  3. 南京Uber优步司机奖励政策(7.20~7.26)

    人民优步奖励前提   *必须满足当周平均评分4.5星及以上,且当周接单率70%及以上,满足以上所有前提即可获得当周奖励 *刷单和红线行为立即封号并取消当周全部奖励及车费! 滴滴快车单单2.5倍,注册地 ...

  4. 2019年猪年海报PSD模板-第五部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1CuZKPmFbbSBvzSXoCt2few                

  5. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  6. php api_token 与 user_token 简析

    前言: --->非开放性平台 --->公司内部产品 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是 ...

  7. 接口测试工具postman(六)添加变量(参数化)

    1.添加全局变量并引用 2.通过设置请求前置配置变量 3.在Tests里面,把响应数据设置为变量 4.添加外部文件,引用外部文件中的变量和数据,此种场景就可以执行多次请求 1)配置文件,txt或者cs ...

  8. ortp代码简析

    ortp初始化 /** *    Initialize the oRTP library. You should call this function first before using *     ...

  9. 如何搭建本地svn服务器和搭建本地Git服务器

    搭建git本地服务器使用的软件有很多,例如:gitlab,gitblit,gitbucket,gogs,gitolite,具体比较:http://softlab.sdut.edu.cn/blog/su ...

  10. unable to access android sdk add-on list and SDK 更新镜像设置

    前记 国内的网络呀,真是操蛋!!!!!! unable to access android sdk add-on list 在 Android Studio 安装目录 bin/idea.propert ...