leetcode—js—Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and 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 解题思路:
(1)本题涉及到链表的结构
(2)本题类似小学数学的加法,从个位开始相加,有超过10则进位,否则就是本节点的值
(3)两个链表的长度不知道谁长,需要做一下处理。 code:
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
var list = new ListNode(0);
var result = list;
var sum, carry = 0;
while(l1 || l2 || carry>0){ //当l1 或 l2 或有进位存在的时候,加法计算就没有结束
sum=0;
if(l1!==null){
sum += l1.val;
l1=l1.next;
}
if(l2!==null){
sum += l2.val;
l2=l2.next;
}
sum += carry;
list.next = new ListNode(sum%10);
carry = parseInt(sum/10);
list = list.next;
}
return result.next; };
leetcode—js—Add Two Numbers的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
随机推荐
- NanoProfiler-Step1翻译
NanoProfiler NanoProfiler is a light weight profiling library written in C# which requires (NanoProf ...
- 1114 记录一点点吧 RP Axure
- RainbowPlan-Alpha版本发布1
博客介绍 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 https:// ...
- AI领域有什么职业?怎样才能在AI领域找到工作?
AI领域是一个很吃香的行业,在这个行业中,很多人都是高薪的,而且有些学生为了以后能够接触到这个行业,都在大学的时候,学习这个专业,那么大家知道AI领域有什么职业吗?下面我们就来给大家讲解一下. 1.算 ...
- iOS开发tip-图片方向
概述 相信稍微接触过iOS图片相关操作的同学都遇到过图片旋转的问题,另外使用AVFoundation进行拍照的话就会遇到前后摄像头切换mirror问题就让人更摸不着头脑了.今天就简单和大家聊一下iOS ...
- laravel 学习笔记 — 神奇的服务容器
2015-05-05 14:24 来自于分类 笔记 Laravel PHP开发 竟然有人认为我是抄 Laravel 学院的,心塞.世界观已崩塌. 容器,字面上理解就是装东西的东西.常见的变量.对象属 ...
- Kubelet 中的 “PLEG is not healthy” 到底是个什么鬼?
原文链接:深入理解 Kubelet 中的 PLEG is not healthy 在 Kubernetes 社区中,PLEG is not healthy 成名已久,只要出现这个报错,就有很大概率造成 ...
- 自学Java第一章——《Java概述》
1.1 Java历史 Java诞生于SUN(Stanford University Network),09年SUN被Oracle(甲骨文)收购. Java之父是詹姆斯.高斯林(James Goslin ...
- [apue] 书中关于伪终端的一个纰漏
在看 apue 第 19 章伪终端第 6 节使用 pty 程序时,发现“检查长时间运行程序的输出”这一部分内容的实际运行结果,与书上所说有出入. 于是展开一番研究,最终发现是书上讲的有问题,现在摘出来 ...
- python pip使用报错: Fatal error in launcher: Unable to create process using '"c:\python27\python.exe" "C:\Python27\Scripts\pip.exe" '
在一个系统中,如果同时存在python2和python3,在cmd.exe程序下执行pip.pip2或者pip3均会报错. 如何解决: 如果是在python3环境下,使用pip安装扩展库,可以使用以下 ...