5 ms·
Padding the array is a bit of a code smell. You can't just pad an array without allocating a new block of memory. After allocation you need to copy the array in
by scrabble 8y ago
Padding the array is a bit of a code smell. You can't just pad an array without allocating a new block of memory. After allocation you need to copy the array into the new space. This is a significant amount of overhead to a fairly straightforward and efficient algorithm.
- arethuza 8y agoWell, that's easy to fix - copy your array into a linked list so you an insert elements at the head efficiently. ;-)
- klyrs 8y agoYes, the real problem with this implementation is the data structure they use. But is `next == null` a special case? ogod what do we do?!
- banku_brougham 8y agoThis is interesting. Is it possible to implement a linked list while eliminating the end of the list ‘edge case’, in the fashion of the parent article?
- pure-awesome 8y agoCreate a "sentinel" node which represents null and also points to itself, perhaps?