I know that Linked-Lists are usually evaluated theoretically, but in practice they are usually ineffective.
I would suggest switching to random access containers to get some speed. The simplest would be an array, but depending on the size of the data we are talking about, a two-line queue or an indexed skip list / B * might be better.
It is clear that it does not change much (yet), however you can go to the given index in O (1) (array, deque) / O (log N) (skip list / B * tree) and not O (N) with a simple associated a list.
And then it's time for magic.
Keith has already revealed the basic idea: instead of actually deleting the column, you just need to mark it as deleted, and then “jump” over it when you go through its structure. However, the hash table requires a linear jump to get to the Nth column. Using the Fenwick tree will provide an efficient way to calculate the real index, and you can jump there right away.
Note that the key benefit of marking a deleted line is the obvious possibility of an undo operation.
Also note that you may need to build a compaction function so that you delete deleted columns from time to time and prevent them from accumulating.
source share