is-circular
Version:
high-performance, zero-dependency circular reference check for objects (or arrays)
29 lines (24 loc) • 585 B
JavaScript
module.exports = Node
/**
* a linked-list node
* @class
* @param {any} value - node's value
* @param {Node} next - next node
*/
function Node (value, next) {
this.value = value
this.next = next
}
/**
* checks if this node or any of its children has the value
* @param {any} value - value to check if linked-list contains
* @return {boolean} true if the list contains the value; false if not
*/
Node.prototype.contains = function (value) {
var cursor = this
while (cursor) {
if (cursor.value === value) return true
cursor = cursor.next
}
return false
}