[Tutor] linked list with cycle structure

Kent Johnson kent37 at tds.net
Wed Jan 7 12:57:21 CET 2009


On Wed, Jan 7, 2009 at 5:59 AM, David Hláčik <david at hlacik.eu> wrote:
> Hello guys,
>
> I have a linked list where *number of elements is unlimited* and
> **last element points on random (can be first, last, in middle ,
> anywhere) element within linked list** - this is important . My goals
> is to create an architecture /scheme for **algoritmus which will count
> total number of elements** of such linked list.

So the list is effectively endless because it has a cycle?

You could walk the list, adding elements (or their ids) to a set,
stopping when you get to an element that is in the set already. Then
the size of the set is the size of the list.

Kent


More information about the Tutor mailing list