题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
let input = readline().split(' ').map(Number) //节点类 class ListNode{ constructor(val){ this.val = val this.next = null } } //链表类 class LinkList{ constructor(val){ this.head = new ListNode(val) this.size = 0 } } //插入 LinkList.prototype.insert = function(value,target){ let node = this.head while(node){ if(node.val==target){ let tmp = new ListNode(value) tmp.next = node.next node.next = tmp break }else{ node = node.next } } } //删除某个值的节点 LinkList.prototype.delete = function(value){ let node = this.head while(node.val==value){ node = node.next } let pre = node let curr = pre.next while(curr){ if(curr.val == value){ pre.next = curr.next curr = pre.next }else{ pre = curr curr = curr.next } } } let list = new LinkList(input[1]) let move = input.slice(2,input.length-1) for(let i=0;i<move.length;i=i+2){ list.insert(move[i],move[i+1]) } list.delete(input[input.length-1]) let res = [] let head = list.head while(head){ res.push(head.val) head = head.next } print(res.join(' '))JavaScript,链表解法