0

This probably sounds a bit dumb but I'm wondering why it won't accept a concerted array such as this submission:

var mergeTwoLists = function(list1, list2) {
  const mergerarr = list1.concat(list2);
  const mergesortfinal = mergerarr.sort((a, b) => a - b);
  return mergesortfinal;
};
1
  • The code challenge starts with this phrase: "You are given the heads of two sorted linked lists"... why would you think "heads of two sorted linked lists" means "arrays"? Commented Aug 16, 2022 at 17:39

2 Answers 2

3

The question (and hence the data structure) is about linked lists, not arrays. The functions you use are for arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

@avariant I might have missed something, but the question was asking on why their code does not work on LeetCode. Isn't my response giving an answer to the question itself? Apologies if that isn't right.
0
var mergeTwoLists = function(l1, l2) {
   var dummy = {
     val : -1,
     next : null
  };
  var curr = dummy;
  while (l1 && l2) {
      if (l1.val > l2.val) {
         curr.next = l2;
         l2 = l2.next;
      } else {
         curr.next = l1;
         l1 = l1.next;
      }
       curr = curr.next;
   }

   curr.next = l1 || l2;

   return dummy.next;
 };

Try this code using JavaScript format at Leetcode you will passed..

1 Comment

Please don't post code only and add an explantation as to why you think that this is the optimal solution. People are supposed to learn from your answer, which might not occur if they just copy paste code without knowing why it should be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.