Skip to content

Instantly share code, notes, and snippets.

@GAierken
Last active September 15, 2020 02:21
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 GAierken/226126a8998ed219de9921aa5cf0c195 to your computer and use it in GitHub Desktop.
Save GAierken/226126a8998ed219de9921aa5cf0c195 to your computer and use it in GitHub Desktop.
const hasCycle = (head) => {
// Set variable to store distinct nodes
let set = new Set()
// for traversing, initialize current with head node
let current = head
//traverse the linked list
while(current){
if(set.has(current)){
// if duplication occurs, return true
return true
}else{
set.add(current)
}
current = current.next
}
// traverse is completed, cycle not found
return false
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment