Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Leetcode 21 Pregunta: fusionar dos listas ordenadas, ¿quiere que devuelva una lista vacía?

Estoy trabajando en esta pregunta en leetcode, https://leetcode.com/problems/merge-two-sorted-lists/

He escrito una solución (ineficiente):

 /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} list1 * @param {ListNode} list2 * @return {ListNode} */ var mergeTwoLists = function (list1, list2) { const arr = []; while (list1 || list2) { if (list1.val !== null) { arr.push(list1.val); } if (list2.val !== null) { arr.push(list2.val); } list1 = list1.next; list2 = list2.next; } arr.sort((a, b) => a - b); arr.forEach((el, index) => { arr[index] = new ListNode(el); if (index > 0) { arr[index - 1].next = arr[index]; } }); if (arr[0]) { return arr[0]; } return new ListNode(null); };

Cuando intento enviar el código, aparece el siguiente error:

 Input: [] [] Output: [0] Expected: []

No entiendo cómo se supone que debo devolver una lista vacía como si estuviera preguntando. Si observa la implementación de ListNode que proporcionan, siempre se crea una instancia de ListNode con un valor de 0 si no está definido.

Como puede ver en la parte inferior de mi respuesta, devuelvo new ListNode(null) si me proporcionan 2 listas vacías, y esto aún da como resultado que el valor sea 0. ¿Alguien puede explicar qué debo hacer para que esta solución pase? ?

Gracias por tu ayuda.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Encontré la solución. Estaba tratando de devolver un nodo de lista, cuando se suponía que debía devolver la cabeza. en lugar de devolver new ListNode(null) , simplemente necesitaba devolver un null . Aquí está la solución:

 /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} list1 * @param {ListNode} list2 * @return {ListNode} */ var mergeTwoLists = function (list1, list2) { const arr = []; while (list1 !== null || list2 !== null) { if (list1) { arr.push(list1.val); list1 = list1.next; } if (list2) { arr.push(list2.val); list2 = list2.next; } } arr.sort((a, b) => a - b); arr.forEach((el, index) => { arr[index] = new ListNode(el); if (index > 0) { arr[index - 1].next = arr[index]; } }); if (arr[0]) { return arr[0]; } return null };
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!