Last active
September 15, 2020 02:21
-
-
Save GAierken/226126a8998ed219de9921aa5cf0c195 to your computer and use it in GitHub Desktop.
This file contains 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
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