Created
March 8, 2017 17:25
-
-
Save javajosh/6b1383fe8b7d5659ab99b26472b52db5 to your computer and use it in GitHub Desktop.
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