2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse orderand 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.
  4.  
  5. 代码:
  1. static void Main(string[] args)
  2. {
  3. ListNode l1 = new ListNode();
  4. l1.next = new ListNode();
  5. l1.next.next = new ListNode();
  6.  
  7. ListNode l2 = new ListNode();
  8. l2.next = new ListNode();
  9. l2.next.next = new ListNode();
  10.  
  11. var res= addTwoNumbers(l1, l2);
  12. while (res != null)
  13. {
  14. Console.Write(res.val);
  15. res = res.next;
  16. }
  17. Console.ReadKey();
  18. }
  19.  
  20. public class ListNode
  21. {
  22. public int val;
  23. public ListNode next;
  24. public ListNode(int x) { val = x; }
  25. }
  26.  
  27. public static ListNode addTwoNumbers(ListNode l1, ListNode l2)
  28. {
  29. ListNode l3 = new ListNode();
  30. ListNode head = l3;
  31. int sum = ;
  32. while (l1 != null || l2 != null)
  33. {
  34. sum = sum > ? : ;
  35. if (l1 != null)
  36. {
  37. sum += l1.val;
  38. l1 = l1.next;
  39. }
  40. if (l2 != null)
  41. {
  42. sum += l2.val;
  43. l2 = l2.next;
  44. }
  45. //存储在l3中
  46. l3.next = new ListNode(sum % );
  47. l3 = l3.next;
  48. }
  49. //判断最后一项是否和大于9,大于则需要再添加一个1.
  50. if (sum > )
  51. {
  52. l3.next = new ListNode();
  53. }
  54. return head.next;
  55. }

解析:

输入:ListNode类型的两个参数

输出:第一个节点。

思想:

  循环链表中的每一位,sum存储两个链表对应位上的和。通过观察不难发现规律,如果上一位和大于9,则下一位初始sum为1,将结果存储在新的链表中。

  最后一位上和大于9时,再多加一位,值为1。

时间复杂度:O(n)

  1.  
  1.  

C# 写 LeetCode Medium #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. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

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

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

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

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

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

  5. 【Leetcode】【Medium】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. LeetCode Linked List Medium 2. Add Two Numbers

    Description You are given two non-empty linked lists representing two non-negative integers. The dig ...

  7. leetcode题解2. Add Two Numbers

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

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

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

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

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

随机推荐

  1. 剑指offer之 二叉搜索树的后续遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution { ...

  2. java入门了解之快捷键

    IDE(Integrated Development Environment ): 集成开发环境,集合开发.运行.调试于一体的一个软件 Eclipse 是一个开放源代码的.基于Java的可扩展开发平台 ...

  3. Codeforces 372B Counting Rectangles is Fun:dp套dp

    题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...

  4. spring boot: 在maven中装入springframework框架

    1.在maven 的pom.xml中加入 <dependency> <groupId>org.springframework</groupId> <artif ...

  5. FFMPEG基于内存的转码实例——输入输出视频均在内存

    我在6月份写了篇文章<FFMPEG基于内存的转码实例>,讲如何把视频转码后放到内存,然后通过网络发送出去.但该文章只完成了一半,即输入的数据依然是从磁盘文件中读取.在实际应用中,有很多数据 ...

  6. BZOJ4317: Atm的树+2051+2117

    BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...

  7. bzoj 3165: [Heoi2013]Segment 线段树

    题目: Description 要求在平面直角坐标系下维护两个操作: 在平面上加入一条线段.记第i条被插入的线段的标号为i. 给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. ...

  8. Ubuntu 14.04开发环境

    安装ssh服务:sudo apt-get install openssh-server 安装vim:sudo apt-get install vim-gtk 安装gparted:sudo apt-ge ...

  9. Oracle 12c 多租户配置和修改 CDB 和 PDB 参数

    1. 配置CDB 实例参数,影响CDB与所有 PDB为CDB配置例程参数相对于对于非CDB的数据库是变化不太.ALTER SYSTEM命令用于设置初始化参数,与使用ALTER DATABASE命令修改 ...

  10. Windows 7下Git SSH 创建Key的步骤

    1.首先你要安装Git工具 下载地址:https://git-scm.com/downloads 2.右键鼠标,选中 “Git Bash here”,当然你也可以在windows的 “开始”---&g ...