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 ...
随机推荐
- Uva1014:Remember the Word
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...
- 【javaScript】加减乘除的精确计算
在js中使用"+"."-"等符号进行运算会出现很大的误差,所以需要自己创建函数进行精确运算. //说明:javascript的加法结果会有误差,在两个浮点数相加 ...
- Java之函数式接口@FunctionalInterface详解(附源码)
Java之函数式接口@FunctionalInterface详解 函数式接口的定义 在java8中,满足下面任意一个条件的接口都是函数式接口: 1.被@FunctionalInterface注释的接口 ...
- Python里的装饰器
装饰器 装饰器是干什么用的? 装饰器可以在不修改某个函数的情况下,给函数添加功能. 形象点来说,从前有一个王叔叔,他一个人住在家里,每天打扫家,看书.于是定义如下一个函数: def uncle_wan ...
- python 学习笔记2 匿名函数
# 匿名函数 lambda a,b : a+b# a.j.from functools import reduce students = [{'name': '张三', 'age': 18, 'hei ...
- Gloang Swagger
功能 自动化生产接口文档 安装 # 安装swaggo get -u github.com/swaggo/swag/cmd/swag # 安装 gin-swagger go get -u github. ...
- 1233: 输出杨辉三角前n行
#include <stdio.h> int main() { int n,i,j,ch[15][15],v,k; char *nl = ""; while(scanf ...
- Java学习笔记----打印基本数据类型范围
/** * Created by N3verL4nd on 2016/11/10. */ public class HelloWorld { public static void main(Strin ...
- SDL初始化和创建窗口
//初始化SDL2和创建一个窗口,并且将屏幕绘制成大红色 #include <iostream> extern "C" { #include <SDL.h> ...
- ubuntu 如何添加alias
公司的nx 上面一般使用gvim 编辑文件.并且为gvim 增加了alias,只要敲 g 就是gvim 的意思,这样编辑一个文件只需要 g xxx.v 就可以了.非常方便. 在自己电脑上安装了ubun ...