最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Why I am getting an error "Output Limit Exceeded" in Javascript? - Stack Overflow

programmeradmin1浏览0评论

I am trying to solve a problem of LinkedList on leetcode in javascript language but out of 86 test cases 74 cases are passed but at 75 I am getting an error Output Limit Exceeded.

Problem:

Given the head of a singly linked list, return true if it is a palindrome.

Examples:

Input: head = [1,2,2,1]
Output: true

Input: head = [1,2]
Output: false

My code:

let array = [];
let list = head;
while (list) {
  array.push(list.val)
  console.log("array1", array)
  list = list.next;
}

let list2 = head;
while (list2) {
  let out = array.pop()
  if (out === list2.val) {
    list2 = list2.next;
  } else {
    return false
  }
}

return true

Last executed input: [3,8,9,3,2,8,9,1,8,9,9,8,5,2,5,4,4,4,3,9,7,5,0,5,8,6,3,3,8,0,7,3,7,7,1,1,1,7,0,2,8,1,8,7,2,9,5,2,9,7,4,8,...]

I am not able to understand why I am getting a Limit exceeded error? Is it due to too many inputs? Or Issue is in my code? If the issue is with inputs then How can I solve this problem in Javascript?

I am trying to solve a problem of LinkedList on leetcode in javascript language but out of 86 test cases 74 cases are passed but at 75 I am getting an error Output Limit Exceeded.

Problem:

Given the head of a singly linked list, return true if it is a palindrome.

Examples:

Input: head = [1,2,2,1]
Output: true

Input: head = [1,2]
Output: false

My code:

let array = [];
let list = head;
while (list) {
  array.push(list.val)
  console.log("array1", array)
  list = list.next;
}

let list2 = head;
while (list2) {
  let out = array.pop()
  if (out === list2.val) {
    list2 = list2.next;
  } else {
    return false
  }
}

return true

Last executed input: [3,8,9,3,2,8,9,1,8,9,9,8,5,2,5,4,4,4,3,9,7,5,0,5,8,6,3,3,8,0,7,3,7,7,1,1,1,7,0,2,8,1,8,7,2,9,5,2,9,7,4,8,...]

I am not able to understand why I am getting a Limit exceeded error? Is it due to too many inputs? Or Issue is in my code? If the issue is with inputs then How can I solve this problem in Javascript?

Share Improve this question edited Apr 18, 2022 at 18:17 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Apr 18, 2022 at 18:13 Harsh MishraHarsh Mishra 9784 gold badges17 silver badges38 bronze badges 2
  • push and pop on an array is probably causing the time issue. You may want to try a different approach. – rcgldr Commented Apr 18, 2022 at 20:25
  • 1 Having a console.log in your loop is going to slow down the process too. – trincot Commented Apr 19, 2022 at 14:02
Add a ment  | 

2 Answers 2

Reset to default 5

console.log is trapped by the LeetCode framework, which sets a limit on the total size of the output you can generate. As your first loop calls it in every iteration, printing the array of already collected values each time, you are producing output in the order of 3

发布评论

评论列表(0)

  1. 暂无评论