RosettaCodeData/Task/Singly-linked-list-Element-insertion/JavaScript/singly-linked-list-element-insertion.js

13 lines
478 B
JavaScript
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
LinkedList.prototype.insertAfter = function(searchValue, nodeToInsert) {
if (this._value == searchValue) {
nodeToInsert.next(this.next());
this.next(nodeToInsert);
}
else if (this.next() == null)
throw new Error(0, "value '" + searchValue + "' not found in linked list.")
else
this.next().insertAfter(searchValue, nodeToInsert);
}
var list = createLinkedListFromArray(['A','B']);
list.insertAfter('A', new LinkedList('C', null));