Skip to content

Instantly share code, notes, and snippets.

@javajosh
Created March 8, 2017 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javajosh/6b1383fe8b7d5659ab99b26472b52db5 to your computer and use it in GitHub Desktop.
Save javajosh/6b1383fe8b7d5659ab99b26472b52db5 to your computer and use it in GitHub Desktop.
function makeList(n){
var head = {'i':0};
var prev = head;
var current;
for (var i = 1; i < n; i++) {
current = {'i': i};
prev.next = current;
prev = current;
};
return head;
}
function printList(list){
var node = list;
while(node.next) {
console.log(node.i);
node = node.next;
}
}
function deleteNode(list, predicate){
var node = list;
while(node.next) {
if (predicate(node)){
node.i = node.next.i;
node.next = node.next.next;
break;
}
node = node.next;
}
}
var list = makeList(10);
deleteNode(list, function(node){return node.i === 5})
printList(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment