【问题标题】:issue with testing Linked List's function测试链表功能的问题
【发布时间】:2021-08-22 06:27:50
【问题描述】:

我正在尝试测试以下链表函数:

function ListNode(x) {
  this.value = x;
  this.next = null;
}

function removeKFromList(l, k) {
    // create node
    let node = new ListNode();
    // assign node.next to the beginning of the given linked list.
    node.next = l;

    // start iterating through the linked list
    let current = node;
    // while there is still a node
    while(current.next) {
        // if the value of the node equals to given K
        if (current.next.value === k) {
            // remove it from the list by hopping from the one node to the next node
            current.next = current.next.next
        } else {
            // move from one node to the next.
            current = current.next;
        }
    }
    //return the linked list
    return node.next;
}
console.log(removeKFromList([3, 1, 2, 3, 4, 5], 3));

我希望输出是:[1, 2, 4, 5] 但是,输出是:[ 3, 1, 2, 3, 4, 5 ]
附言我想指出我不擅长与链接列表相关的问题,因此,如果我遗漏了一些“明显”的东西,请不要怪我。

【问题讨论】:

  • 使用调试器单步调试您的代码。
  • @tgdavies,能否请您更具体一点,至于我究竟应该经历哪一部分?

标签: javascript data-structures linked-list


【解决方案1】:

您的removeKfromList 函数是正确的,但它希望它的第一个参数是一个链表,并且它返回一个对链表的引用。

但是你用一个数组来调用它,并且期望输出是一个数组。

我猜你对一些代码挑战网站感到困惑,它们从标准输入中获取输入,格式化为数组,但在实际调用解决方案代码之前先将该输入转换为链表。

为了使它在该站点的框架之外工作,您首先必须自己将数组转换为链表,然后在调用该函数后,您需要将结果转换回数组或任何可打印的东西。

所以下面我添加了两个实用函数来做到这一点——我没有触及你提供的代码:

function ListNode(x) {
    this.value = x;
    this.next = null
}

function removeKFromList(l, k) {
    // create node
    let node = new ListNode();
    // assign node.next to the beginning of the given linked list.
    node.next = l;

    // start iterating through the linked list
    let current = node;
    // while there is still a node
    while(current.next) {
        // if the value of the node equals to given K
        if (current.next.value === k) {
            // remove it from the list by hopping from the one node to the next node
            current.next = current.next.next
        } else {
            // move from one node to the next.
            current = current.next;
        }
    }
    //return the linked list
    return node.next;
}

// All changes are in the part below:

function arrayToList(arr) {
    return arr.reduceRight((next, val) => 
        Object.assign(new ListNode(val), { next })
    , null);
}

function listToArray(list) {
    const arr = [];
    for (let node = list; node; node = node.next) {
        arr.push(node.value);
    }
    return arr;
}

const list = arrayToList([3, 1, 2, 3, 4, 5]);
const shorter = removeKFromList(list, 3);
const result = listToArray(shorter);
console.log(result);

【讨论】:

  • 感谢您的出色回答!真的很感谢你的回答!是的,你是对的,这个问题来自 codesignal(在 leetcode 等网站上也有类似的问题);出于某种原因,他们没有提供(或要求用户提供)完整代码来测试他们的答案!
【解决方案2】:

你可能不知道 LinkedList 的含义。首先,您创建了一个node,然后将其next 分配为node.next = l。这里l是列表,不是节点,不能调用node.next.next或者current.next.next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多