Skip to content

Instantly share code, notes, and snippets.

@jeankueo
Created October 23, 2016 09:55
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 jeankueo/4a28c84db5cf1b8fb386b7f4c6b73c61 to your computer and use it in GitHub Desktop.
Save jeankueo/4a28c84db5cf1b8fb386b7f4c6b73c61 to your computer and use it in GitHub Desktop.
Create a linked list, with method add and contain
function Node (next) {
this.next = next;
}
function LinkedList () {
this.add = function (node) {
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
};
this.contain = function (node) {
var cursor = this.head;
while (cursor) {
if (cursor === node) {
return true;
}
cursor = cursor.next;
}
return false;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment