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.
Will still work. Imagine there are only two elements [head,tail] and tail.next points to head. Then on the second iteration of while loop, slow==fast==head and you return 1. You can show that in general you will detect the loop of tail back to head on the n-th iteration of the while loop where n is the total number of elements.
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 →
Will still work. Imagine there are only two elements [head,tail] and tail.next points to head. Then on the second iteration of while loop, slow==fast==head and you return 1. You can show that in general you will detect the loop of tail back to head on the n-th iteration of the while loop where n is the total number of elements.