1. 请看题目描述:
  1.  

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.

  1.  

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

  1. //Definition for singly-linked list.
  1. class ListNode
  2. {
  3. public int val;
  4. public ListNode next;
  5. public ListNode(int x)
  6. {
  7. val = x;
  8. }
  1. 10 }
  1.  分析:
      这个两数相加不难,只是两个正整数都放在了链表里面了而已。还有一点是,整数的每一位数字都是放过来存放的,那么这样就明显降低了难度了,实现起来就像我们小学的时候列树式计算两个数的和,从低位加起,和与10的余数放在本位,和与10的商产生高位进位。在本题只需从两个链表的表头开始相加,用一个存放计算结果的链表的对应位置存放两个链表对应位置的整数和进位和除以10的余数,并用临时变量存放进位,用于加入下一次的两个整数之和中。直到两个链表中长的链表也遍历完了,那么结果链表也就出来了。结果链表的整数的各位数字也是反序的。
  2.  
  3.   从前面的分析来看我们可以使用递归和非递归两种算法实现。
      C#参考实现如下:  
  1.  递归实现:
  1. public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
  2. {
  3. return AddTwoNumbers(l1, l2, );
  4. }
  5. public ListNode AddTwoNumbers(ListNode l1, ListNode l2 , int carry)
  6. {
  7. if(l1 == null && l2 == null && carry ==)
  8. {
  9. return null;
  10. }
  11. ListNode result = new ListNode();
  12. int sum = (l1 == null ? : l1.val) + (l2 == null ? : l2.val) + carry;
  13. result.val = sum % ;
  14. ListNode more = AddTwoNumbers( l1 == null ? null : l1.next ,
  15. l2 == null ? null :l2.next,
  16. sum >= ? : );
  17. result.next = more;
  18. return result;
  19. }
  1.  
  1. 非递归实现:
  1. public ListNode AddTwoNumbers2(ListNode l1, ListNode l2)
  2. {
  3. ListNode node = new ListNode();
  4. ListNode result = node;
  5.  
  6. int carry = ;
  7. while(l1 != null || l2 != null || carry != )
  8. {
  9. int sum =( l1 ==null ? : l1.val )+( l2 == null ? : l2.val )+ carry;
  10. l1 =( l1 ==null ? null : l1.next);
  11. l2 =( l2 == null ? null : l2.next);
  12.  
  13. node.next = new ListNode(sum % );
  14. node = node.next;
  15.  
  16. carry = sum / ;
  17. }
  18. return result.next;
  19. }
  1.  
  1.  总结:这个题目不难只要静下心来就可以想出来,涉及的细节问题也不多。那么如果把题目改一下,改成两个链表中存放正整数的每一位数字是正向存放的,又该如何实现呢?注意:链表是单向的。这时就要注意两个整数的位数长短了。我们可以这样实现:a)先比较两个链表的长度并用零填充较短的链表 b)递归结果加到首部
      

                            

简单的两数之和再次乱入<< Add Two Numbers >>的更多相关文章

  1. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

  2. 【Leetcode】【简单】【1. 两数之和】【JavaScript】

    题目描述 1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能 ...

  3. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  4. LeetCode题解001:两数之和

    两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  5. LeetCode1——两数之和

    最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...

  6. 653. 两数之和 IV - 输入 BST + HashSet

    653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...

  7. leetcode算法1.两数之和

    哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...

  8. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  9. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

随机推荐

  1. 熟练使用git命令

    git工作流程图: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...

  2. JSON对象如何转化为字符串?

    序列化 定义 指将 JavaScript 值转化为 JSON 字符串的过程. JSON.stringify() 能够将 JavaScript 值转换成 JSON 字符串.JSON.stringify( ...

  3. VC++编译zlib

    目录 第1章简介    1 第2章版本1.2.3    2 2.1 编译汇编代码    2 2.1.1 32位汇编    2 2.1.2 64位汇编    5 2.2 Visual C++ 6.0   ...

  4. sessionStorage

    for(i=0;i<tkt.length;i++){ if(tkt[i].redEnvelopeType==1){ full1+="<div class='pic_01' id= ...

  5. H5网页播放器播不了服务器上的mp4视频文件

    打开IIS,在功能视图里找到MIME类型菜单,打开该菜单后鼠标右键添加.mp4扩展名的MIME类型video/mp4 其他视频文件播放不了估计也得在IIS里添加对应的MIME类型(从服务器下载文件时也 ...

  6. addEventListener,attachEvent

    addEventListener是js填加事件:用法如下: target.addEventListener(type,listener,useCapture) target: 文档节点.documen ...

  7. JavaWeb基础: Tomcat

    Tomcat目录层次结构 Tomcat本质上是一个Java应用,要启动Tomcat服务需要安装JRE并配置JRE的Path,才能通过Tomcat_HOME/bin/startup脚本正常启动Tomca ...

  8. 父类中“this” 指向问题

    “this.字段”如果出现在父类代码中,指的就是父类属性. “this.方法”不管出现在父类还是子类代码中,指的都是子类方法. “this.字段”如果出现在子类代码中,指的就是子类属性. 在程序的时候 ...

  9. Search for a Range [LeetCode]

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  10. javascript function new this

    1. 首先,我们这里把function直接调用时将这个function当做方法来看待,而new function是将function当做类来看待 2. 当把function作为类来使用时,functi ...