You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Tags:Linked List, Math

分析:逐位相加,考虑进位。

  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. public:
  11. ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
  12. if (!l1 || !l2) {
  13. return NULL;
  14. }
  15. int carry = ; //进位
  16. ListNode * result = new ListNode();
  17. ListNode * first = result; //追踪结果链表的当前节点
  18. ListNode * pre = NULL; //追踪结果链表的前一个节点
  19. //当l1和l2均没有超过链表末尾节点时
  20. while (l1 && l2) {
  21. first->val = (carry + l1->val + l2->val) % ;
  22. carry = (carry + l1->val + l2->val) / ;
  23. if (pre == NULL)
  24. pre = first;
  25. else {
  26. pre->next = first;
  27. pre = first;
  28. }
  29. first = new ListNode();
  30. l1 = l1->next;
  31. l2 = l2->next;
  32. }
  33. //当l1和l2都超过链表末尾节点时
  34. if (!l1 && !l2) {
  35. if (carry == ) {
  36. first->val = carry;
  37. pre->next = first;
  38. }
  39. return result;
  40. }
  41. //当l1超过末尾而l2尚未超过时
  42. if (!l1 && l2) {
  43. while (l2) {
  44. first->val = (carry + l2->val) % ;
  45. carry = (carry + l2->val) / ;
  46. if (pre == NULL)
  47. pre = first;
  48. else {
  49. pre->next = first;
  50. pre = first;
  51. }
  52. first = new ListNode();
  53. l2 = l2->next;
  54. }
  55. if (carry == ) {
  56. first->val = ;
  57. pre->next = first;
  58. }
  59. return result;
  60. }
  61. //当l2超过末尾而l1尚未超过时
  62. if (!l2 && l1) {
  63. while (l1) {
  64. first->val = (carry + l1->val) % ;
  65. carry = (carry + l1->val) / ;
  66. if (pre == NULL)
  67. pre = first;
  68. else {
  69. pre->next = first;
  70. pre = first;
  71. }
  72. first = new ListNode();
  73. l1 = l1->next;
  74. }
  75. if (carry == ) {
  76. first->val = ;
  77. pre->next = first;
  78. }
  79. return result;
  80. }
  81.  
  82. }
  83. };

LeetCode Algorithm 02_Add Two Numbers的更多相关文章

  1. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  2. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

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

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

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  5. LeetCode Algorithm 05_Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  8. LeetCode 2 Add Two Numbers 模拟,读题 难度:0

    https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...

  9. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

随机推荐

  1. 这是一套Java菜鸟到大牛的学习路线之高级教程,由工作了10年的资深Java架构师整理。

    这是一套Java菜鸟到大牛的学习路线之高级教程,由工作了10年的资深Java架构师整理.        01-java高级架构师设计-基础深入        J2SE深入讲解        Java多 ...

  2. CF(438D) The Child and Sequence(线段树)

    题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...

  3. Netty In Action中文版 - 第七章:编解码器Codec

    http://blog.csdn.net/abc_key/article/details/38041143 本章介绍 Codec,编解码器 Decoder,解码器 Encoder,编码器 Netty提 ...

  4. android 动态设置TextView值,例:金额添加

    一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...

  5. jQuery Easy UI ProgressBar(进度条)组件

    ProgressBar(进度条)组件,这个还是挺好玩的.我们在自己做点什么的时候常常能用到,比方上传下载文件.导入导出文档啊.加载网页等等. 应用场景非常多,使用起来还非常easy. 演示样例: &l ...

  6. Unity3D:粒子系统Particle System

    1. GameObject → Create Other  →  Particle System. 2. 选中 Particle System,可看到下列屬性: 3.Particle System: ...

  7. Python 面向对象与 C++、Java 的异同

    1. 子类是否自动调用父类的构造方法 C++.Java 会在子类对象的构造中自动首先调用父类的构造: Python 则相对啰嗦一点: 如果子类不覆盖父类的__init__()方法,则子类默认将执行与父 ...

  8. 关于node的fs路径问题

    我在写一个静态网页的服务器中遇到的一个问题,当时没理解就去查了 因为要访问最外部的json文件,就定义了一个模块读取文件,然后在外边的server.js中调用 但是一直路径错误. 我相信很多人和我一样 ...

  9. Android 通过OnScrollListener来监听RecyclerView的位置

    最近做一个漫画app,在阅读漫画界面需要通过获取recyclerView的位置来实时更新界面上的图片进度(比如1/9), 查阅资料得知了可以通过LayoutManager来获取recyclerView ...

  10. host---域名查询

    host命令是常用的分析域名查询工具,可以用来测试域名系统工作是否正常. 选项 -a:显示详细的DNS信息: -c<类型>:指定查询类型,默认值为“IN“: -C:查询指定主机的完整的SO ...