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

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

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    const list = (await readline()).split(" ").map(Number);
    const ListNode = (value,next = null) => ({value,next});
    const n = list.shift();//节点数
    const head = ListNode(list.shift());//头结点
    const deleteValue = list.pop();//要删除的节点的值
    while(list.length>1){
        const first = list.shift();
        const second = list.shift();
        let node = head;
        while(node && node.value!=second) node = node.next;
        const newNode = ListNode(first);
        newNode.next = node.next;
        node.next = newNode;
    }
    let node = head,res =[];
    while(node){
        if(node.value != deleteValue) res.push(node.value);
        node = node.next;
    }
    console.log(res.join(" "));
}()

全部评论

相关推荐

求实习的小白1213:华科去这 你是真敢去啊
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务