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. LINQ 学习路程 -- 查询操作 Quantifier Operators All Any Contain

    Operator Description All 判断所有的元素是否满足条件 Any 判断存在一个元素满足条件 Contain 判断是否包含元素 IList<Student> studen ...

  2. linux常见命令2

    systemctl set-hostname HOSTNAME  在centos7上设置主机名,永久有效 curl -O -L http://www.gnu.org/software/gettext/ ...

  3. 【转载】Dom4j的使用(全而好的文章)

    Dom4j的使用(全而好的文章) Dom4j 使用简介 作者:冰云 icecloud(AT)sina.com 时间:2003.12.15   版权声明: 本文由冰云完成,首发于CSDN,未经许可,不得 ...

  4. mysql 启动服务错误

    在博客上看到下面这个文档解决了问题.推荐可以看下. http://blog.csdn.net/yaowuliu/article/details/51133279

  5. C/C++ 库函数 是否调用 WinAPI

    1. 跟了一个函数 fopen,简单测试代码为: #include<stdio.h> #define F_PATH "e:\\Z.txt" int main(void) ...

  6. priority_queue用法(转载)

    关于priority_queue 1,关于STL中的priority_queue:确定用top()查看顶部元素时,该元素是具有最高优先级的一个元素. 调用pop()删除之后,将促使下一个元素进入该位置 ...

  7. VC6++常用快捷键

    VC6快捷键大全(转载) VC6快捷键大全,记在这里,方便查阅.F1: 帮助Ctrl+O :OpenCtrl+P :PrintCtrl+N :NewCtrl+Shift+F2 :清除所有书签F2 :上 ...

  8. Parallel Programming-多消费者,多生产者同时运行并行

    在上一篇文章演示了并行的流水线操作(生产者和消费者并行同时执行),C#是通过BlockingCollection这个线程安全的对象作为Buffer,并且结合Task来实现的.但是上一篇文章有个缺陷,在 ...

  9. Mybatis新版实践

    配置文件节点顺序 MyBatis的configuration节点需要有顺序,首先是propertes然后是settings,environment... @Param注解参数 对于Mapper接口,如 ...

  10. php命名空间(namespace)内如何使用系统类

    作者:ffsystem 使用命名空间,可以更方便的组织代码,以及代码复用.新写的一个项目引入了命名空间. 简介:使用namespace,使用__autoload自动导入类. 今天将以前的一段代码,加入 ...