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:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807. 代码:
static void Main(string[] args)
{
ListNode l1 = new ListNode();
l1.next = new ListNode();
l1.next.next = new ListNode(); ListNode l2 = new ListNode();
l2.next = new ListNode();
l2.next.next = new ListNode(); var res= addTwoNumbers(l1, l2);
while (res != null)
{
Console.Write(res.val);
res = res.next;
}
Console.ReadKey();
} public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
} public static ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
ListNode l3 = new ListNode();
ListNode head = l3;
int sum = ;
while (l1 != null || l2 != null)
{
sum = sum > ? : ;
if (l1 != null)
{
sum += l1.val;
l1 = l1.next;
}
if (l2 != null)
{
sum += l2.val;
l2 = l2.next;
}
//存储在l3中
l3.next = new ListNode(sum % );
l3 = l3.next;
}
//判断最后一项是否和大于9,大于则需要再添加一个1.
if (sum > )
{
l3.next = new ListNode();
}
return head.next;
}

解析:

输入:ListNode类型的两个参数

输出:第一个节点。

思想:

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

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

时间复杂度:O(n)


 

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. Docker学习(三)

    查看docker daemon服务运行状态 service docker status

  2. jQuery横向手风琴图片滑块

    jQuery横向手风琴图片滑块是一款非常不错的jQuery特效横向手风琴图片滑块插件,可以自动播放,也可以鼠标滑过时切换.+ 欢迎喜欢的朋友下载研究 源码下载页:http://www.huiyi8.c ...

  3. a note of R software write Function

    Functionals “To become significantly more reliable, code must become more transparent. In particular ...

  4. 关于React前端构建的一般过程 - 理论篇

    概要 本文以个人阅读实践经验归纳前端架构构建过程,以Step by Step方式说明创建一个前端项目的过程.并会对每个阶段所使用的技术进行可替代分析,如Express替换Hapi或者Koa的优缺点分析 ...

  5. 动态规划 两个字符串之间的编辑距离 leetcode72

    public static int minDistance(String word1, String word2) { char[] s1 = word1.toCharArray(); char[] ...

  6. I.MX6 USB Camera

    /************************************************************************* * I.MX6 USB Camera * 说明: ...

  7. error: ‘errno’ was not declared in this scope

    问题: 将一个c文件改为cpp文件,其中的perror()改用C++中的std::cerr << strerror(error) << std::endl;来替换. 重新编译文 ...

  8. js之__proto__原型链

    可参考: http://blog.csdn.net/irelandken/article/details/7297490

  9. 优化Java堆大小的5个技巧

    本文作者Pierre是一名有10多年经验的高级系统架构师,他的主要专业领域是Java EE.中间件和JVM技术.根据他多年的工作实践经验,他发现许多性能问题都是由Java堆容量不足和调优引起的.下面他 ...

  10. [MySQL]关于Com_状态

    MySQL 5.5官方文档: http://dev.mysql.com/doc/refman/5.5/en/server-status-variables.html#statvar_Com_xxx C ...