Given a linked list of N nodes. The task is to check if the the linked list has a loop. Linked list can contain self loops. 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)