合并两个列表 LeetCode

问题描述 投票:0回答:2

我已经在Repl.it网站上解决了这个问题,但是当我在LeetCode上提交代码时,它给出了一个typeError,我将其粘贴到这里:

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

这是代码:

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;
};

我真的不知道问题出在哪里......如果有人可以帮助我,我将不胜感激

javascript node.js
2个回答
7
投票

这是一个Linked List合并问题,而不是常规的数组合并。这会通过:

/**
 * 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;
};

这就是您的列表的样子:

/**
 * 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}
 */

参考文献

  • 有关更多详细信息,您可以查看讨论区。有很多公认的解决方案,具有各种语言和解释、高效的算法,以及渐近时间/空间复杂性分析12

如果您正在准备面试


0
投票

你已经完成了,

L1[1,2] + L2[3,4] = L1L2[1,2,3,4]

如果我们有L1

    L1{
        val:1,
        next:{
            val:2,
            next: null
        }
    }

我们有L2

    L2{
        val:3,
        next:{
            val:4,
            next: null
        }
    }

我们要发展L1 + L2

    L1L2{
        val:1,
        next:{
            val:2,
            next: {
                val:3,
                next: {
                    val:4,
                    next:null
                }
            }
        }
    }

然后使用JS我们可以通过以下方式完成,

function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val);
    this.next = (next === undefined ? null : next);
}

function mergeTwoLists(l1, l2) {
    let temp = new ListNode(0);
    let current = temp;

    while (l1 !== null && l2 !== null) {
        if (l1.val < l2.val) {
            current.next = l1;
            l1 = l1.next;
        } else {
            current.next = l2;
            l2 = l2.next;
        }
        current = current.next;
    }

    // If one of the lists is longer than the other
    if (l1 !== null) {
        current.next = l1;
    } else {
        current.next = l2;
    }

    return temp.next;
}

// Example usage
const L1 = new ListNode(1, new ListNode(2, null));
const L2 = new ListNode(3, new ListNode(4, null));

const result = mergeTwoLists(L1, L2);
© www.soinside.com 2019 - 2024. All rights reserved.