C# 写 LeetCode Medium #2 Add Two Numbers
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的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Linked List Medium 2. Add Two Numbers
Description You are given two non-empty linked lists representing two non-negative integers. The dig ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
随机推荐
- 如何处理异常? catch Exception OR catch Throwable
在Java中,当你需要统一处理异常的时候,你是会选择catch (Exception),还是直接catch (Throwable)? Java的异常体系 Throwable: Java中所有异常和错误 ...
- Java -- 乒乓球 乒乓弹球游戏
<疯狂Java讲义> 练习游戏 import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; impo ...
- jsp的9个内置对象
Jsp提供了request.response.session.application.out.page.config.exception.pageContext9个内置对象. 1. Request R ...
- django实现用户注册、登录、退出
视图 from django.contrib import auth from django.contrib.auth.models import User from django.views.dec ...
- python3 字符串属性(四)
1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...
- 三 Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性
Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性 Views(视图函数)逻辑处理,最终是围绕着两个对象实现的 http请求中产生两个核心对象: http请求:HttpRe ...
- SpringBoot_03_依赖本地jar
一.方法一 1.说明 用Maven打到本地仓库,然后直接引入 2.参考资料 Springboot 打Jar包,Maven完美解决本地Jar包自动打入Springboot Jar包中 3.执行maven ...
- git branch detached from jb4.2.2_1.0.0-ga
/*************************************************************************** * git branch detached f ...
- 流媒体直播服务器:Bull-Live-Server
Bull Live Server 简称 BLS ,旨在使用C++语言提供强大功能和高性能的流媒体直播服务器. 为何要写 BLS ? 1.simple rtmp server https://githu ...
- [冬令营模拟]GTSG2018
上学期没有去 GTSG,于是今天老师让我们来做一下 GTSG2018 Day1 & Day3 Day1 在上午当成一场考试来搞了,Day3 由于锅太多而且 T3 玄学而被放到下午自学... 上 ...