xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub! LeetCode & linked list bug

add-two-numbers

xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
LeetCode & linked list bug

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 发布文章使用:只允许注册用户才可以访问!