Created
October 23, 2016 09:55
-
-
Save jeankueo/4a28c84db5cf1b8fb386b7f4c6b73c61 to your computer and use it in GitHub Desktop.
Create a linked list, with method add and contain
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 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