一、题目要求

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.

二、我的解法及其错误之处

由于english比较low,理解上述题目还是花了点时间。

题目看懂了,确实不难,涉及结构体、指针,求和。

然后就开工,直接在线写代码,编译通过,但是提交后报错了:

1.第一次错误是Runtime Error,具体错误是

signed integer overflow: 1000000000000000000 * 10 cannot be represented in

2.第二次错误AddressSanitizer: heap-use-after-free on address 0x602000000118 at pc 0x000000462f75 bp 0x7fff9680bfd0 sp 0x7fff9680bfc8

后来仔细考虑了一下,我做的过程是:

将链表转换为一个整数(用了long long),然后求和,最后转换为一个链表返回。

这个题目,我考虑复杂了。错误之处在于链表表示的数,可以非常大,也可以是0。

下面是我的错误代码:

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
long long n1 = 0;
long long n2 = 0;
long long result = 0;
long long t = 1;
ListNode *p = l1;
while(p != NULL){
n1 = n1 + t* p->val;
t = t* 10;
p = p->next;
} p = l2;
t = 1;
while(p != NULL){
n2 = n2 + t* p->val;
t = t * 10;
p = p->next;
}
result = n1 + n2; ListNode * pHead = NULL;
if(result == 0){
return pHead = new ListNode(0);
}
while(result>0){
if(pHead == NULL){
pHead = new ListNode(result % 10);
}else{
p = pHead;
while(p->next !=NULL){
p = p ->next;
}
p->next = new ListNode(result % 10);
} result = result / 10;
}
return pHead;
}
};

直接链表对应为求和,然后返回链表就可以了,这个反而更简单。完整的代码如下:

#include<iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode head(0),*curr = & head;
int remain=0,tmp;
while(l1!=NULL && l2!=NULL){
tmp = l1->val + l2->val + remain;
curr -> next = new ListNode(tmp % 10);
curr = curr->next;
l1 = l1->next;
l2 = l2->next;
remain = tmp / 10;
}
while(l1 !=NULL){
tmp = l1->val + remain;
curr->next = new ListNode(tmp % 10);
curr = curr->next;
l1 = l1->next;
remain = tmp / 10;
}
while(l2 !=NULL){
tmp = l2->val + remain;
curr->next= new ListNode(tmp % 10);
curr = curr->next;
l2 = l2->next;
remain = tmp /10;
}
if(remain !=NULL){
curr->next = new ListNode(remain);
}
return head.next;
}
}; int main(){
Solution s;
ListNode * l1,*l2,*curr; //初始化 l1 2->4->3
l1= new ListNode(2);
curr = l1;
curr->next = new ListNode(4);
curr = curr->next;
curr->next = new ListNode(3);
curr = curr->next; //初始化 l2 5->6->4
l2= new ListNode(5);
curr = l2;
curr->next = new ListNode(6);
curr = curr->next;
curr->next = new ListNode(4);
curr = curr->next; ListNode * l3 = s.addTwoNumbers(l1,l2); //输出结果
curr = l3;
while(curr!=NULL){
cout<<curr->val<<" ";
curr= curr->next;
} return 0;
}

刷题2. Add Two Numbers的更多相关文章

  1. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  2. 【LeetCode刷题系列 - 002题】Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  3. LeetCode第四题,Add Two Numbers

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

  4. 【LeetCode每天一题】Add Two Numbers(两链表相加)

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

  5. LeetCode第二题:Add Two Numbers

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

  6. LeetCode刷题笔录Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

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

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

  8. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  9. 周刷题第一期总结(two sum and two numbers)

    由于深深的知道自己是事件驱动型的人,一直想补强自己的薄弱环节算法,却完全不知道从哪里入手.所以只能采用最笨的办法,刷题.从刷题中遇到问题就解决问题,最后可能多多少少也能提高一下自己的渣算法吧. 暂时的 ...

随机推荐

  1. 关于华为高斯数据库 GaussDB 版本及认证体系介绍

    目录 你需要知道的 技术有国界 从它的名称说起 你听到过的版本 你听到过的流言蜚语 各个版本的区别 版本未来名称 华为 GaussDB 认证体系介绍 GaussDB 其他资料相关链接 你需要知道的 任 ...

  2. String类型的日期怎么转化为Date类型

    在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tablename where rownum<3 order ...

  3. Pwnable.kr

    Dragon —— 堆之 uaf 开始堆的学习之旅. uaf漏洞利用到了堆的管理中fastbin的特性,关于堆的各种分配方式参见堆之*bin理解 在SecretLevel函数中,发现了隐藏的syste ...

  4. vue 脚手架使用路由

    概念: 后端路由: 后端处理URL和页面之间的映射关系 前端发展阶段: 1.后端渲染阶段(以jsp,php为代表,其特点为html代码和后端语言代码混写在一起 2.前后端分离阶段(随着ajax的兴起, ...

  5. C++&C语言 -> //有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

    /*    a                  b                          c       d 1  5                   5               ...

  6. ECMAScript基本语法——⑤运算符 逻辑运算符

    &&与,会短路:左边为false右边就不参与运算||或,会短路:左边为true右边就不参与运算!非, 注意:在JavaScript中,如果运算数不是运算符要求的类型,那么JavaScr ...

  7. 845. 八数码(bfs+map)

    在一个3×3的网格中,1~8这8个数字和一个“X”恰好不重不漏地分布在这3×3的网格中. 例如: 1 2 3 X 4 6 7 5 8 在游戏过程中,可以把“X”与其上.下.左.右四个方向之一的数字交换 ...

  8. AcWing 7. 混合背包问题

    #include<iostream> #include<algorithm> #include<cstring> using namespace std ; ; i ...

  9. 诡异的Integer

    先看下面的这个代码,为什么同样的都是赋值,却得不到同样的结果,也没有超出int的范围啊?这是为什么? package ppt_test; public class StrangeIntegerBeha ...

  10. 实用技巧之while里面使用getchar或sleep函数

    我们经常需要打印一些变量的取值来调试程序,使用while(1)是常用的手段. ) { char letter = getchar(); printf("test_point is %d \t ...