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

asp.net - Session has Expired in IE 10,9,8 -

PHP Error "Undefined index" -

C# List<> to xml -