We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
det stores the node that were visited in the set. the first if statement checks to see if the linked list is empty hence why he checls if it is equal to the nullptr/NULL. the while loop checks the whole linked list to see if the element is there. Here we use the set property of no repeated element. That is an element is already in the set then a Node* is returned that is not at the tail of the loop. If the element is not in the set then it will return the tail. So the if statement in the while loop returns anything from det that is not the tail then we return true since it would mean that there was a repeated element.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
det stores the node that were visited in the set. the first if statement checks to see if the linked list is empty hence why he checls if it is equal to the nullptr/NULL. the while loop checks the whole linked list to see if the element is there. Here we use the set property of no repeated element. That is an element is already in the set then a Node* is returned that is not at the tail of the loop. If the element is not in the set then it will return the tail. So the if statement in the while loop returns anything from det that is not the tail then we return true since it would mean that there was a repeated element.