Removing the last element from a LinkedList is unnecessarily slow
tom-churchill opened this issue · 2 comments
tom-churchill commented
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
sarah27h commented
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?
parhamsaremi commented
#23
i tried to remove the redundant line and fix the function