Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Two Lists LeetCode

I've solved the problem on Repl.it website, but when I submitted the code on LeetCode it gave a typeError, that I'm going to paste it here:

Line 29 in solution.js
         throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type 
ListNode");
         ^
TypeError: [] is not valid value for the expected return type ListNode
Line 29: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 13: Char 26 in solution.js (Object.<anonymous>)
Line 1200: Char 30 in loader.js (Module._compile)
Line 1220: Char 10 in loader.js (Object.Module._extensions..js)
Line 1049: Char 32 in loader.js (Module.load)
Line 937: Char 14 in loader.js (Function.Module._load)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Line 17: Char 47 in run_main_module.js

And here's the code:

var mergeTwoLists = function(l1, l2) {
  let i = 0, j = 0;
  var out = [];
  while(i < l1.length || j < l2.length) {
    if(j == l2.length || i < l1.length && l1[i] < l2[j]) {
      out.push(l1[i++]);
    } else {
      out.push(l2[j++]);
    }
  }
  return out;
};

I really don't know where the problem is... if someone could help I would be grateful

like image 336
JellySquare2 Avatar asked Jul 12 '20 06:07

JellySquare2


People also ask

How do you combine two linked lists in order?

(1) Create a new head pointer to an empty linked list. (2) Check the first value of both linked lists. (3) Whichever node from L1 or L2 is smaller, append it to the new list and move the pointer to the next node. (4) Continue this process until you reach the end of a linked list.

How do I merge two unsorted linked lists?

Below are the steps: Concatenate the two lists by traversing the first list until we reach it's a tail node and then point the next of the tail node to the head node of the second list. Store this concatenated list in the first list. Sort the above-merged linked list.

How do I combine two arrays?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

How do I merge two unsorted arrays in sorted order?

Write a SortedMerge() function that takes two lists, each of which is unsorted, and merges the two together into one new list which is in sorted (increasing) order. SortedMerge() should return the new list.


1 Answers

This is a Linked List merge question, not a regular array merging. This'd pass through:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
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;
};

This is how your list would look like:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */

References

  • For additional details, you can see the Discussion Board. There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time/space complexity analysis1, 2 in there.

If you are preparing for interviews:

  • We'd write bug-free and clean codes based on standards and conventions (e.g., c1, 2, c++1, 2, java1, 2, c#1, 2, python1, javascript1, go1, rust1).
like image 152
Emma Avatar answered Sep 29 '22 20:09

Emma