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.
No matter the size of the list, if it has a cycle, it will eventually iterate more than 100 times. Imagine a list with one node that has itself as a next node. It has a cycle and will loop 100 times and my algorithm will know it has a cycle. This is obviously not an optimal solution, however it is a very simple one. Also, since it is guaranteed to run in O(100) it is actually a constant time solution.
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 →
No matter the size of the list, if it has a cycle, it will eventually iterate more than 100 times. Imagine a list with one node that has itself as a next node. It has a cycle and will loop 100 times and my algorithm will know it has a cycle. This is obviously not an optimal solution, however it is a very simple one. Also, since it is guaranteed to run in O(100) it is actually a constant time solution.