doubly-linked-list icon indicating copy to clipboard operation
doubly-linked-list copied to clipboard

`remove(node)`, `addPrevious(node, nodeData)`, `addNext(node, nodeData)` are missing

Open popovitsj opened this issue 2 years ago • 1 comments

Hi,

I was considering to use this library, but it seems to miss three very important methods on its API, namely a way to insert or remove a node not by providing the index, but by providing the node itself.

This should be constant time for a doubly linked list and is a very important feature of this datastructure when used to optimize runtime complexity in algorithms.

popovitsj avatar Jun 19 '22 17:06 popovitsj

I also needed removeGivenNode and restoreGivenNode functions. I created a new class locally and extended this LinkedList with the following implementation:

        removeGivenNode: function (node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
        },

        restoreGivenNode: function (node) {
            node.prev.next = node;
            node.next.prev = node;
        },

This removes given node in constant time. If you hold onto the node, you can also put it back into the list.

Hope this helps!

truszko1 avatar Sep 18 '22 05:09 truszko1