find whether linked list has cycle javascript code example
Example: figure out if a linked list contains a cycle javascript
function LinkedListNode(value) {
this.value = value;
this.next = null;
}
let head = new LinkedListNode(10)
head.next = new LinkedListNode(25)
head.next.next = new LinkedListNode(46)
function hasCycle(head) {
let node = head;
let map={};
while(node){
if(map[node.value]){
return {"Found":node}
}
else{
map[node.value] = true;
}
node = node.next;
}
return "Not found";
}
hasCycle(head)