javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

Maybe there are some redundent code in Doubly-Linked-List

Open ten-ltw opened this issue 4 years ago • 1 comments

    reverse() {
        let node = this.head,
            previous = null,
            next = null;

        while (node) {
            next = node.next;
            console.log(previous?.value);
            previous = node.previous;
            console.log(previous?.value);
            node.next = previous;            
            node.previous = next;

            previous = node;
            node = next;
        }
        this.tail = this.head;
        this.head = previous;
        return this;
    }

I think the code previous = node.previous; in while loop is redundent code. I try this code and the previous.vaule always get a same value.

ten-ltw avatar Feb 24 '21 08:02 ten-ltw

Yes ,the previous=node.previous statement is a redundant code. We can simply remove it and it doesn't affect the result.

ihemanthm avatar Jan 27 '24 17:01 ihemanthm