Created
December 14, 2013 00:03
-
-
Save jasondscott/7953840 to your computer and use it in GitHub Desktop.
This file contains 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 Node(d) { | |
this.next = undefined; | |
this.data = d; | |
this.appendToTail = function(d) { | |
var end = new Node(d); | |
var n = this; | |
while (n.next !== undefined) { n = n.next; } | |
n.next = end; | |
}; | |
this.removeDups = function() { | |
var hash = []; | |
var current = this; | |
var prev; | |
while (current !== undefined) { | |
if(hash[current.data]) { | |
//remove | |
prev.next = current.next; | |
} else { | |
hash[current.data] = true; | |
prev = current; | |
} | |
current = current.next; | |
} | |
}; | |
this.print = function() { | |
console.log("------"); | |
var n = this; | |
console.log(n.data); | |
while (n.next !== undefined) { | |
n = n.next; | |
console.log(n.data); | |
} | |
console.log("------"); | |
}; | |
} | |
var root = new Node(5); | |
root.appendToTail(3); | |
root.appendToTail(3); | |
root.appendToTail(3); | |
root.appendToTail(3); | |
root.appendToTail(3); | |
root.appendToTail(4); | |
root.appendToTail(7); | |
root.appendToTail(4); | |
root.appendToTail(1); | |
root.appendToTail(3); | |
root.print(); | |
root.removeDups(); | |
root.print(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment