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

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

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

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...

  2. 【8086汇编-Day3】用debug做实验时的技巧与坑

    Ⅰ· 无病呻吟 学一门语言,不动手实验是学不好的,在实验中不断遇坑然后解决,才有进益.所以写一下我在第一次汇编实验中的所思所想(王爽<汇编语言>第二章章末实验). Ⅱ · 实验内容 题解思 ...

  3. 20145202马超《java》实验四

    实验指导:http://www.cnblogs.com/lxm20145215----/p/5444207.html 实验指导:http://www.cnblogs.com/Vivian517/p/6 ...

  4. 2 引用 深copy 浅copy

    1. is == 总结 is 是比较两个引用是否指向了同一个对象(引用比较). == 是比较两个对象是否相等. In [1]: a = [11,22,33] In [2]: b = [11,22,33 ...

  5. springBoot Swagger2 接口文档生成

    // 生成配置类 package com.irm.jd.config.swagger; import org.springframework.context.annotation.Bean; impo ...

  6. PHP中strtotime()的使用

    strtotime是一个非常强大的函数. 传入的参数,详见官网的介绍 本月最后一个周日 echo date('Y-m-d',strtotime('last sunday of this month') ...

  7. 关于mongodb的mapReduce

    由于nodejs本身的限制,在程序中使用js进行大批量计算效率不高.而V8引擎自身对内存大小的限制(64位系统下1.4G),同样限制了数据规模. 因此,相对于从mongodb中抽出数据进行计算,在mo ...

  8. 前端开发工程师 - 02.JavaScript程序设计 - 第1章.基础篇

    第1章--基础篇 JS介绍 html 网页的内容:css 网页的样式:javascript 网页的行为 i.e. hello world <!DOCTYPE html> <html& ...

  9. [CodeForce455A]Boredom

    题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...

  10. lintcode: Check Sum of Square Numbers

    Check Sum of Square Numbers Given a integer c, your task is to decide whether there're two integers ...