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 ...
随机推荐
- 剑指offer之 二叉搜索树的后续遍历序列
题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution { ...
- java入门了解之快捷键
IDE(Integrated Development Environment ): 集成开发环境,集合开发.运行.调试于一体的一个软件 Eclipse 是一个开放源代码的.基于Java的可扩展开发平台 ...
- Codeforces 372B Counting Rectangles is Fun:dp套dp
题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...
- spring boot: 在maven中装入springframework框架
1.在maven 的pom.xml中加入 <dependency> <groupId>org.springframework</groupId> <artif ...
- FFMPEG基于内存的转码实例——输入输出视频均在内存
我在6月份写了篇文章<FFMPEG基于内存的转码实例>,讲如何把视频转码后放到内存,然后通过网络发送出去.但该文章只完成了一半,即输入的数据依然是从磁盘文件中读取.在实际应用中,有很多数据 ...
- BZOJ4317: Atm的树+2051+2117
BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...
- bzoj 3165: [Heoi2013]Segment 线段树
题目: Description 要求在平面直角坐标系下维护两个操作: 在平面上加入一条线段.记第i条被插入的线段的标号为i. 给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. ...
- Ubuntu 14.04开发环境
安装ssh服务:sudo apt-get install openssh-server 安装vim:sudo apt-get install vim-gtk 安装gparted:sudo apt-ge ...
- Oracle 12c 多租户配置和修改 CDB 和 PDB 参数
1. 配置CDB 实例参数,影响CDB与所有 PDB为CDB配置例程参数相对于对于非CDB的数据库是变化不太.ALTER SYSTEM命令用于设置初始化参数,与使用ALTER DATABASE命令修改 ...
- Windows 7下Git SSH 创建Key的步骤
1.首先你要安装Git工具 下载地址:https://git-scm.com/downloads 2.右键鼠标,选中 “Git Bash here”,当然你也可以在windows的 “开始”---&g ...