LeetCode & linked list bug

add-two-numbers

shit test

/**
* 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) {
const log = console.log;
function ListNode(val, next) {
this.val = 0 || val;
this.next = null || next;
}
log(`l1`, l1)
log(`l2`, l2)
const arr1 = [];
const arr2 = [];
while(l1) {
arr1.push(l1.val);
l1 = l1.next;
}
while(l2) {
arr2.push(l2.val)
l2 = l2.next;
}
const sum = parseInt(arr1.reverse().join(``)) + parseInt(arr2.reverse().join(``));
// 807
const arr = Array.from(sum + ``).reverse().map(i => parseInt(i));
// [7, 0, 8]
const LinkedList = (value) => {
const node = new ListNode(value, ``);
if(!head) {
head = node;
} else {
let current = head;
while(current.next) {
current = current.next;
}
current.next = node;
}
};
let head = null;
for (let i = 0; i < arr.length; i++) {
LinkedList(arr[i]);
}
log(`head`, head)
return head;
// return arr;
}; /* 好垃圾的测试呀,为什么参数给的不是 ListNode, 而是 array! 答案没有毛病呀 l1 [2,4,3]
l2 [5,6,4]
head ListNode {
val: 7,
next: ListNode { val: 0, next: ListNode { val: 8, next: '' } }
} */

refs

https://leetcode.com/problems/add-two-numbers/

https://leetcode-cn.com/problems/add-two-numbers/submissions/

https://leetcode-cn.com/submissions/detail/99618022/



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


LeetCode & linked list bug的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. LeetCode 全解(bug free 训练)

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  3. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. LeetCode——Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  8. LeetCode——Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

随机推荐

  1. NoClassDefFoundError: javax/xml/bind/DatatypeConverter错误原因以及解决办法

    nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter 报错内容: org.sprin ...

  2. OPTIONS的预请求(Preflighted Request)

    OPTIONS的预请求(Preflighted Request) Ajax 请求中出现OPTIONS(Request Method: OPTIONS)_qiao-CSDN博客 https://blog ...

  3. redis6.0多线程

    https://www.sohu.com/a/331991216_268033 执行还是单线程     读写解析多线程   6.0 https://segmentfault.com/a/1190000 ...

  4. qbxt 学习笔记 10.2 晚

    目录 整除性 素数 组合数 Lucas 定理: 整除性 直接搬 ppt 特殊的整除性质 素数 素数定理: 线性筛: 原理:一个合数只由其最大素因子筛去. 代码: 组合数 Lucas 定理: \[\bi ...

  5. 五万字长文带你学会Spring

    Sping Spring概念介绍 spring是啥呢,你在斗地主的时候把别人打爆了那叫spring, 你成功的追到了你爱慕已久的女神,人生中的春天来了,那也叫sping 好了别看我老婆了,咱来讲讲啥是 ...

  6. vscode 刚安装运行cnpm命令报错

    平时的开发工具什么都用,最近手贱把vscode卸载掉了,然而重新安装时,自已以前的什么配置都没了~~~~~~,又开始从头搞起,但是一切安装配置完毕,执行cnpm命令时报错,晕!!!!!! 解决办法:执 ...

  7. HTML5 网页制作技巧

    本文总结自由人民邮电出版社出版的<HTML.CSS.Javascript网页制作>. 总结进行学习,并分享给同样编写HTML5的朋友. 1:背景音乐的添加 <embed src=&q ...

  8. 最新Ceph L版与openstack Pike对接

    安装Ceph luminous   实验环境 三台服务器,每台服务器都有4块硬盘,每台服务器都将自己的第一块硬盘作为系统盘,剩下的做ceph   一.在所有服务器上操作 #使用阿里源 yum inst ...

  9. centos7系统忘记root密码

    第一步:开启服务器,在如下界面键入"e"进入编辑 第二步:找到以linux16开头的一行,在本行行尾键入"rd.break" 第三步:输入完成后,按" ...

  10. spring MVC 3.2中@ResponseBody(Post接口)返回乱码的完美解决方案

    本来因为ajax跨域http远程调用时有问题,在服务端响应时用以下方式解决了,但IE8及下有问题. response.addHeader("Access-Control-Allow-Orig ...