Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As others noted you can't know how many elements there are supposed to be in the list. But you can guesstimate.

1. Take a guess about how much memory the program has allocated so far. If you can't, go with 2^48 = 281474976710656 bytes - the maximum you can do on a 64bit pc today.

2. Divide by the minimum size of a list entry.

3. Traverse the list until the end or until you traverse more elements than there ever could be.

In general though, as noted, you'll know if you're making a list that could possibly contain cycles due to exposing an interface that allows the creation of cycles. In that case, given that you want to be able to efficiently detect cycles, you'd make a special linked list implementation that does keep internal count of its size.



Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: