Since LinkedList is a nightmare in terms of memory overhead and also slower when iterating elements, I would like to have something like int poiter data structure.
So I have a list of ints and I form it by inserting elements at the beginning-middle. When the list is ready, I iterate it in a one-by-one fashion from the left. That is my problem constraint.
I would profile and try different data structures.
That being said, why not try to use
ArrayList; it’s fast to iterate and even insert, while in O(n), is fast because moving consecutive data in a cache is lightning fast. (If you also reverse the list while inserting it will be even faster!)Update, since my fellow programmers doesn’t understands why I recommend
ArrayListfor this perticular problem:The problem isn’t inserting elements. In that case the linked list wins direcly (O(1) against the O(n) for the array list). The problem is to find the place to insert them. In this case the
LinkedListis really slow. There is a huge overhead to find the place to insert since it doesn’t allocate the memory in a “good matter”. Lots of cache misses!OP hasn’t posted any code so we only know what OP wants but here is an algorithm analysis for the two cases (where C* are constants):
LinkedList, for each element to insert:ArrayList, for each element to insert:So, both algorithms are in O(n) but looking closely at the constants: C1 is really big (explained above). C2 and C3 on the other hand are small because iterating and moving within the cache is fast.
So, if you have enough memory try an
ArrayList!