data structures - Checking if the linked list is circular -


i reading tortoise , hare (slow , fast runner) algorithm here, don't understand why it's considered best solution.

wouldn't less time consuming this:

  • save root node

  • travel through linked list

  • at each new node, check if it's root node.

just realized circular list doesn't need connecting head. can have loop somewhere in middle. makes 2 "runners" necessary.

but if checking explicitly "snake eating own tail" kind of linked list, enough check equality of pointer root node, suggested before


Comments

Popular posts from this blog

How to logout from a login page in asp.net -

Stack level too deep error after upgrade to rails 3.2 and ruby 1.9.3 -