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.
operations on lists are O(n) on lists but average O(1) on sets, while adding to both is O(1). Therefore, as written this algorithm is O(n^2), but would be O(n) with a set.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cycle Detection
You are viewing a single comment's thread. Return to all comments →
You should use a set rather than a list. If you check the wiki, you'll see that
operations on lists are O(n) on lists but average O(1) on sets, while adding to both is O(1). Therefore, as written this algorithm is O(n^2), but would be O(n) with a set.