[leetcode]Add Two Numbers——JS实现
Javascript的结构体应用,如下:
function station(name, latitude, longitude){
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
}
var s1 = new station('station1', 100, 200);
console.log(s1.name+" 's latitude :" + s1.latitude );
/**
* 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 head = new ListNode(0);
var temp1 = 0;
var temp2 = 0;
var val1;
var val2;
while(l1 || l2 || temp1) {
if (l1)val1 = l1.val;
else val1 = 0;
if (l2) val2 = l2.val;
else val2 = 0;
temp2 = Math.floor((val1 + val2 + temp1) % 10);
temp1 = Math.floor((val1 + val2 + temp1) / 10);
head.val += 1;
var newNode = new ListNode(temp2);
if(head.next == null){
head.next = newNode;
}
else {
var tempNode = head.next;
while(tempNode.next != null)
tempNode = tempNode.next;
tempNode.next = newNode;
}
if (l1)l1 = l1.next;
if (l2)l2 = l2.next;
}
return head.next;
};
[leetcode]Add Two Numbers——JS实现的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
随机推荐
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...
- sublime —— 强大的插件
1. 代码 自动补全与智能提示: All Autocomplete:Sublime Text 默认的 Autocomplete 功能只考虑当前的文件,而 All Autocomplete 插件会搜索所 ...
- 洛谷 P2296 寻找道路 —— bfs
题目:https://www.luogu.org/problemnew/show/P2296 第一次用 Emacs 对拍,写了半天: 注意那个 is 赋值的地方很容易错,千万别反复赋值: 一道水题写了 ...
- DTV_SI 汇总 & 兼谈LCN
前言 本章主要对数字广播DVB做一个系统的概况的描述,以及一些spc的相关的内容,虽然流程分析的不多,但是做为后续 章节资料的源泉,也是不可或缺的. 一. ATSC和DVB数字电视系统的比较 本文的主 ...
- [转] 本地项目上传github (新项目 / 旧项目)
前置:安装Git Bash,在github上新建仓库repository 1.右键点击项目所在文件夹,运行: git bash here.在git bash窗口运行命令 git init 把这个目录变 ...
- EasyUI 取得选中行数据
转自:http://www.jeasyui.net/tutorial/23.html 本实例演示如何取得选中行数据. 数据网格(datagrid)组件包含两种方法来检索选中行数据: getSelect ...
- 炫酷的 CSS 形状(值得收藏)
在今日头条中看到炫酷的 CSS 形状,就记录一下: 1.圆形 #circle { width: 100px; height: 100px; background: red; border-radius ...
- css实现左边div固定宽度,右边div自适应撑满剩下的宽度
(1)使用float <div class="use-float"> <div></div> <div></div> & ...
- 8 种提升ASP.NET Web API性能的方法
ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...
- 洛谷P4158 [SCOI2009]粉刷匠
传送门 设$dp[i][j][k][0/1]$表示在涂点$(i,j)$,涂了$k$次,当前点的颜色是否对,最多能刷对多少个格子 首先换行的时候肯定得多刷一次 然后是如果和前一个格子颜色相同,那么当前点 ...