Skip to content

Instantly share code, notes, and snippets.

@jasondscott
Created December 14, 2013 00:03
Show Gist options
  • Save jasondscott/7953840 to your computer and use it in GitHub Desktop.
Save jasondscott/7953840 to your computer and use it in GitHub Desktop.
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