Created
May 28, 2015 23:03
-
-
Save javajosh/79fac13800ad7509c3f5 to your computer and use it in GitHub Desktop.
A fancy deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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