Bug fix patch of the double link list
asbai opened this issue · comments
asbai commented
The pre and next properties of the node separated by the remove
method are not correctly set to null. This will cause the linked list to be disrupted when performing operations such as moveToHead
.
The fix patch:
DoubleLinkedList.prototype.remove = function(node) {
if (node.pre) {
node.pre.next = node.next;
} else {
this.headNode = node.next;
}
if (node.next) {
node.next.pre = node.pre;
} else {
this.tailNode = node.pre;
}
node.pre = node.next = null; // <---- Set to null
};
DoubleLinkedList.prototype.insertBeginning = function(node) {
node.pre = null; // <---- Set to null
if (this.headNode) {
node.next = this.headNode;
this.headNode.pre = node;
return this.headNode = node;
} else {
return this.headNode = this.tailNode = node;
}
};
asbai commented
Sorry, I confused this project to another :-(