leetcode-algorithms-2 Add Two Numbers

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.

解法

同时遍历两个链表,对值进行相加.得到的值%10就是新的链表的值,同时存下/10(只可能是1)的值,用于下个位数的增值.

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution
  10. {
  11. public:
  12. ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
  13. {
  14. ListNode *l = nullptr;
  15. ListNode *lt = nullptr;
  16. ListNode *node1 = l1;
  17. ListNode *node2 = l2;
  18. int lastaddval = 0;
  19. while(true)
  20. {
  21. if (node1 == nullptr && node2 == nullptr) break;
  22. int nodeval1 = 0;
  23. int nodeval2 = 0;
  24. if (node1 != nullptr)
  25. {
  26. nodeval1 = node1->val;
  27. node1 = node1->next;
  28. }
  29. if (node2 != nullptr)
  30. {
  31. nodeval2 = node2->val;
  32. node2 = node2->next;
  33. }
  34. int sumval = nodeval1 + nodeval2 + lastaddval;
  35. lastaddval = sumval / 10;
  36. if (l == nullptr)
  37. {
  38. l = new ListNode(sumval % 10);
  39. lt = l;
  40. }
  41. else
  42. {
  43. ListNode *n = new ListNode(sumval % 10);
  44. lt->next = n;
  45. lt = n;
  46. }
  47. }
  48. if (lastaddval != 0)
  49. {
  50. ListNode *n = new ListNode(lastaddval);
  51. lt->next = n;
  52. lt = n;
  53. }
  54. return l;
  55. }
  56. };

时间复杂度: O(max(m,n)).m和n分别是l1和l2的长度.

空间复杂度: O(max(m,n)).

链接: leetcode-algorithms 目录

leetcode-algorithms-2 Add Two Numbers的更多相关文章

  1. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  2. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  3. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. C# 写 LeetCode Medium #2 Add Two Numbers

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

  5. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

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

  6. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

  7. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

  8. leetcode题解2. Add Two Numbers

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

  9. 【LeetCode练习题】Add Two Numbers

    链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  10. LeetCode OJ 2. Add Two Numbers

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

随机推荐

  1. [JavaScript] - replaceAll,将字符串中的字母或数字等全部替换掉的方式

    原题 function DNAtoRNA(dna) { // create a function which returns an RNA sequence from the given DNA se ...

  2. HBase与列存储

    传统的行存储和(HBase)列存储的区别 1.为什么要按列存储 列式存储(Columnar or column-based)是相对于传统关系型数据库的行式存储(Row-basedstorage)来说的 ...

  3. Python中的垃圾回收机制

    Python的垃圾回收机制 引子: 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存给回收掉,而变量名是访问到变量值的唯一方式,所以 ...

  4. 8、nginx和tengine简介

    练习: 使用nginx反向代理(rr调度)用户请求至两个以上的后端LAMP(按标准路径部署的有pma,wd),不管用户请求是什么内容都反向代理至后端服务器去,但是如果用户请求的是图片或者是html,就 ...

  5. Python 基础 Python是什么

    1.Python 是一门高级的.面向对象的,解释性,脚本语言.

  6. super()、this属性与static静态方法的执行逻辑

    1.super的构造顺序:永远优先构造父类的方法 2.static永远在类实例之前执行,this的使用范围为实例之后

  7. 【selenium2】【unittest】

    #栗子 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver ...

  8. _itemmod_extra_equipments_enable

    如果这个表是空的 所有装备都可以当成双甲  如果这个表里有配置物品 那么只有表里的 才可以当成双甲 表说明 `comment` 备注 `entry` 物品ID

  9. 《EM-PLANT仿真技术教程》读书笔记

    1.在系统分析过程中,必须考虑系统所处的环境,因此划分系统与环境的边界是系统分析的首要任务 2.模型可以分为物理模型和数学模型.数学模型可以分为解析模型.逻辑模型.网络模型以及仿真模型.模型可以分为离 ...

  10. Android 错误集合

    1. Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 erro ...