Buckets-JS icon indicating copy to clipboard operation
Buckets-JS copied to clipboard

Removing the last element from a LinkedList is unnecessarily slow

Open tom-churchill opened this issue 4 years ago • 2 comments

Removing the first element from a LinkedList is fast, however, removing the last element is slow.

The problem seems to be that in the removeElementAtIndex function rather than simply accessing the lastNode element, the nodeAtIndex is called, fetching the second to last element, which is done by iterating over the entire list.

Removing the last element from a LinkedList is very common, so it being crippled severely hampers the usability of the data structure.

Thanks

tom-churchill avatar Jul 29 '19 18:07 tom-churchill

Hi @tom-churchill,

I faced the same issue while I was implementing the removeLastNode method for LinkedList, Do you find another solution for this problem?

sarah27h avatar Sep 01 '19 17:09 sarah27h

https://github.com/mauriciosantos/Buckets-JS/pull/23 i tried to remove the redundant line and fix the function

parhamsaremi avatar Dec 20 '20 16:12 parhamsaremi