题解 | #从单向链表中删除指定值的节点#

从单向链表中删除指定值的节点

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
rl.on("line", function (line) {
    const tokens = line.split(" ");
    tokens.shift() // 第一个不要
    const delChar = tokens.pop();
    const head = tokens.shift();
    let left = 0;
    let right = 1;
    const linkList = [head]
    while(right < tokens.length){
        const cur = tokens[left];
        const target = tokens[right];
        const index = linkList.indexOf(target);
        linkList.splice(index+1,0,cur)
        left +=2;
        right +=2;

    }
    console.log(linkList.filter(i=>i!=delChar).join(' '))
});



全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务