delete node in linked list
Anónimo
class Node { constructor(value) { this.value = value this.next = null; } } let first = new Node(1); first.next = (new Node(4)); first.next.next = (new Node(5)); first.next.next.next = (new Node(7)); //lets delete 5 class NodeWithDelete extends Node { findAndDeleteNodeByValue (value) { let objectLookIn = Object.assign({}, this); //so i don;t have to change tihs context :) while(objectLookIn.next !== null) { if(objectLookIn.next.value === value) { //just remove the pointer next and point to the next one after it :) if(objectLookIn.next.next) { objectLookIn.next = objectLookIn.next.next; } else { objectLookIn.next = null; } break; } objectLookIn = objectLookIn.next; } return objectLookIn; } } var firstTwo = new NodeWithDelete ( 0 ); firstTwo.next = Object.assign({}, first); // for shallow copy console.log(firstTwo.findAndDeleteNodeByValue( 4 )); console.log(firstTwo) console.log(first);