Add Two Numbers 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/add-two-numbers/description/


Description

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


  1. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
  2. Output: 7 -> 0 -> 8
  3. Explanation: 342 + 465 = 807.

Solution

  1. class Solution {
  2. private:
  3. ListNode* copyList(ListNode* listHeadNode) {
  4. if (listHeadNode == NULL)
  5. return NULL;
  6. ListNode* resHeadNode = new ListNode(listHeadNode -> val);
  7. ListNode* resCurNode = resHeadNode;
  8. ListNode* listCurNode = listHeadNode -> next;
  9. while (listCurNode != NULL) {
  10. resCurNode -> next = new ListNode(listCurNode -> val);
  11. resCurNode = resCurNode -> next;
  12. listCurNode = listCurNode -> next;
  13. }
  14. return resHeadNode;
  15. }
  16. public:
  17. ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  18. if (l1 == NULL)
  19. return copyList(l2);
  20. if (l2 == NULL)
  21. return copyList(l1);
  22. bool isOverflow = false;
  23. int curVal = l1 -> val + l2 -> val;
  24. if (curVal >= 10) {
  25. isOverflow = true;
  26. }
  27. ListNode* resHeadNode = new ListNode(curVal % 10);
  28. ListNode* resPreNode = resHeadNode;
  29. ListNode* l1CurNode = l1 -> next;
  30. ListNode* l2CurNode = l2 -> next;
  31. while (l1CurNode != NULL && l2CurNode != NULL) {
  32. curVal = l1CurNode -> val + l2CurNode -> val;
  33. if (isOverflow)
  34. curVal++;
  35. resPreNode -> next = new ListNode(curVal % 10);
  36. isOverflow = (curVal / 10 > 0);
  37. resPreNode = resPreNode -> next;
  38. l1CurNode = l1CurNode -> next;
  39. l2CurNode = l2CurNode -> next;
  40. }
  41. if (l1CurNode == NULL)
  42. resPreNode -> next = copyList(l2CurNode);
  43. else
  44. resPreNode -> next = copyList(l1CurNode);
  45. if (isOverflow) {
  46. if (resPreNode -> next == NULL) {
  47. resPreNode -> next = new ListNode(1);
  48. } else {
  49. while (true) {
  50. if (resPreNode -> next == NULL) {
  51. resPreNode -> next = new ListNode(1);
  52. break;
  53. } else {
  54. curVal = resPreNode -> next -> val + 1;
  55. if (curVal >= 10) {
  56. resPreNode -> next -> val = curVal % 10;
  57. resPreNode = resPreNode -> next;
  58. } else {
  59. resPreNode -> next -> val = curVal;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. return resHeadNode;
  67. }
  68. };

解题描述

这道题可以说解决的是数字相加的问题,但是由于数字的长度不确定,输入的数字可能是现有的内置数据类型无法存储的,所以像题目中给出的方法是,利用链表来记录数字,然后针对链表中的每一个节点的值进行相加。

明确了题目的要求之后,很快就可以清楚,题目最需要解决的问题就是链表边界和相加进位的问题。我的做法算是简单的对特殊情况打补丁。

[Leetcode Week15] Add Two Numbers的更多相关文章

  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] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

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

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

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

  8. LeetCode之Add Two Numbers

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

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

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

随机推荐

  1. Redis 学习之主从复制

    该文使用centos6.5 64位    redis3.2.8 主从复制 Redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进 ...

  2. MyBatis原理简介

    1.什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyB ...

  3. BZOJ 1103 大都市(dfs序+树状数组)

    应该是一道很水的题吧... 显然可以用树链剖分解决这个问题,虽然不知道多一个log会不会T.但是由于问题的特殊性. 每次修改都是将边权为1的边修改为0,且询问的是点i到根节点的路径长度. 令点i到根节 ...

  4. C++中Set的使用

    /* #include <string> // 使用 string 类时须包含这个文件 #include <iostream> // 这个就加上去吧.c++的输入和输出. us ...

  5. POJ3648:Wedding——题解(配2-SAT简易讲解)

    http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...

  6. BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...

  7. 洛谷 P1291 [SHOI2002]百事世界杯之旅 解题报告

    P1291 [SHOI2002]百事世界杯之旅 题目描述 "--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...

  8. 全面解析JavaScript的Backbone.js框架中的Router路由

    这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...

  9. Mysql 语句优化技巧

    前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...

  10. 后端日期类属性date 不接受string类型日期,都是没找到解决的方法,所有前端传回的string字符串都一一转化为java定义的类型

    1.比如日期 我们可以是yyyy-MM-dd 亦可以是 yyyy-MM-dd HH:mm:ss 方法1在java代码中需要的字段上加上注解 写上日期类型,不过这样很麻烦,每个人写了日期类型的接收前端的 ...