LeetCode2.两数相加 JavaScript
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
/**
* 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) {
//定义一个新链表res,一个temp的链表cur,用来当作res的指针,一个进位标志carry
let res = new ListNode(-1),
cur = res,
carry = 0;
while(l1 !== null || l2 !== null){
//取到两个链表当前的数值
let num1 = l1 == null ? 0 : l1.val;
let num2 = l2 == null ? 0 : l2.val;
//求和
let sum = num1 + num2 + carry;
//对进位标志的验证
carry = sum >= 10 ? 1 : 0;
cur.next = new ListNode(sum % 10);
cur = cur.next;
l1 = l1 ? l1.next : l1;
l2 = l2 ? l2.next : l2;
}
if (carry === 1) {
cur.next = new ListNode(1);
}
return res.next; };
LeetCode2.两数相加 JavaScript的更多相关文章
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- Leetcode2 两数相加 Python
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- Leetcode2.两数相加——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入两个链表,相同位置相加,进位给下一个位置,输出相加后的链表 { ListNode *res=) ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- BNU4286——Adjacent Bit Counts——————【dp】
Adjacent Bit Counts Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- 【Shell】shell的运算
一.除法 a=12 b=7 1) expr $a / $b 计算出结果为个1 ,只支持整除 2) echo "scale=2;$a/$b" | bc结果为 1.71 3) awk ...
- attribute和property的区别
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- scss-传递内容块到@mixin
样式块被传递给混入用于放置内的样式.在@content指令的位置,样式被包含进mixin. 内容块被传递到块被定义一个混合的范围进行计算. 下面的例子演示了mixin使用内容块的SCSS代码: @mi ...
- js获取css的各种样式并且设置他们
js原生获取css样式,并且设置,看似简单,其实并不简单,我们平时用的ele.style.样式,只能获取内嵌的样式,但是我们写的样式基本都在style属性里面; 这里我们就需要: 下面这个代码主要是设 ...
- 使用Android Studio搭建Android开发环境
一.Android Studio简单介绍 2013年GoogleI/O大会首次发布了Android Studio IDE(Android平台集成开发环境).它基于Intellij IDEA开发环境,旨 ...
- 常见HTTP状态200,304,403,404,503
下面提供 HTTP 状态码的完整列表.您也可以查看 W3C官方HTTP 状态码说明(英文). 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求 ...
- scope 作用域(bean 的生存范围)
默认是 singleton ,单例模式,如下代码: @Test public void testAddUser() throws Exception { ApplicationContext ctx ...
- 在crontab中执行脚本重要事项
crontab不能成功执行shell脚本的可能原因 crond进程不存在,该进程是crontab的守护进程,它必须存在才能让crontab正常使用: 系统时间不对: 环境变量的问题:crontab执行 ...
- VC简单嵌入汇编
转自:http://blog.csdn.net/arcsinsin/article/details/8126473 内嵌汇编的使用方法是: __asm { 语句 } 你可以把它插入程序中 ...