javascript-algorithms
javascript-algorithms copied to clipboard
Maybe there are some redundent code in Doubly-Linked-List
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.
Yes ,the previous=node.previous statement is a redundant code. We can simply remove it and it doesn't affect the result.