题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
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(" ")); }()