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 ...
随机推荐
- ubuntu遇到了 dpkg was interrupted, you must manually run 'dpkg..的问题
dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. E: _cache- ...
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 移动端下滑刷新插件(jQuery插件)
由于在工作不能独自开发,而且为了给他们方便,自己写过不少的插件,不过今天刚好空闲,发出刚好完成的,移动端的下滑到底刷新插件.我不是很喜欢写插件给别人用,因为用起来自然是简单的,没什么难度,所以一起分享 ...
- WinSock 完成端口模型
之前写了关于Winsock的重叠IO模型,按理来说重叠IO模型与之前的模型相比,它的socket即是非阻塞的,也是异步的,它基本上性能非常高,但是它主要的缺点在于,即使我们使用历程来处理完成通知,但是 ...
- 项目在低版本浏览器下不兼容?友情提示客户升级浏览器(以下只针对IE浏览器)
(function (window) { var win = window, sys = {}, ua = navigator.userAgent.toLowerCase(); (/msie\s+(\ ...
- java常用API之基本类型包装类
基本类型包装类概述: 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的. 而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型. 想实现字符串与基本数据之间转换,需 ...
- mysql二:库操作
一.系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等 performance_schema: My ...
- SQLAlchemy的使用---M2M多对多关系
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, Stri ...
- 牛客提高R5 A.同余方程
题意 题目链接 Sol 设\(solve(x, y)\)表示\(i \in [0, x], j \in [0, y]\)满足题目要求的方案数 首先容斥一下,\(ans = solve(r_1, r_2 ...
- js时间与时间戳之间的转换操作,返回天、小时、分,全家桶
1.将时间戳转换成时间 var formatDate = function(d) { var now = new Date(d); var year = now.getFullYear(); var ...