mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 18:10:24 +08:00
Minor code style fixes for DoublyLinkedList.
This commit is contained in:
@ -78,38 +78,44 @@ export default class DoublyLinkedList {
|
|||||||
let deletedNode = null;
|
let deletedNode = null;
|
||||||
let currentNode = this.head;
|
let currentNode = this.head;
|
||||||
|
|
||||||
do {
|
while (currentNode) {
|
||||||
if (this.compare.equal(currentNode.value, value)) {
|
if (this.compare.equal(currentNode.value, value)) {
|
||||||
deletedNode = currentNode;
|
deletedNode = currentNode;
|
||||||
|
|
||||||
if (deletedNode === this.head) {
|
if (deletedNode === this.head) {
|
||||||
|
// If HEAD is going to be deleted...
|
||||||
|
|
||||||
// Set head to second node, which will become new head.
|
// Set head to second node, which will become new head.
|
||||||
this.head = deletedNode.next;
|
this.head = deletedNode.next;
|
||||||
|
|
||||||
// Set new head's previous to null
|
// Set new head's previous to null.
|
||||||
if (this.head) {
|
if (this.head) {
|
||||||
this.head.previous = null;
|
this.head.previous = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all the nodes in list has same value that is passed as argument
|
// If all the nodes in list has same value that is passed as argument
|
||||||
// then all nodes will get deleted, therefore tail needs to be updated
|
// then all nodes will get deleted, therefore tail needs to be updated.
|
||||||
if (deletedNode === this.tail) {
|
if (deletedNode === this.tail) {
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
}
|
}
|
||||||
} else if (deletedNode === this.tail) {
|
} else if (deletedNode === this.tail) {
|
||||||
// set tail to second last node, which will become new tail
|
// If TAIL is going to be deleted...
|
||||||
|
|
||||||
|
// Set tail to second last node, which will become new tail.
|
||||||
this.tail = deletedNode.previous;
|
this.tail = deletedNode.previous;
|
||||||
this.tail.next = null;
|
this.tail.next = null;
|
||||||
} else {
|
} else {
|
||||||
|
// If MIDDLE node is going to be deleted...
|
||||||
const previousNode = deletedNode.previous;
|
const previousNode = deletedNode.previous;
|
||||||
const nextNode = deletedNode.next;
|
const nextNode = deletedNode.next;
|
||||||
|
|
||||||
previousNode.next = nextNode;
|
previousNode.next = nextNode;
|
||||||
nextNode.previous = previousNode;
|
nextNode.previous = previousNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNode = currentNode.next;
|
currentNode = currentNode.next;
|
||||||
} while (currentNode);
|
}
|
||||||
|
|
||||||
return deletedNode;
|
return deletedNode;
|
||||||
}
|
}
|
||||||
@ -149,8 +155,12 @@ export default class DoublyLinkedList {
|
|||||||
*/
|
*/
|
||||||
deleteTail() {
|
deleteTail() {
|
||||||
if (!this.tail) {
|
if (!this.tail) {
|
||||||
|
// No tail to delete.
|
||||||
return null;
|
return null;
|
||||||
} else if (this.head === this.tail) {
|
}
|
||||||
|
|
||||||
|
if (this.head === this.tail) {
|
||||||
|
// There is only one node in linked list.
|
||||||
const deletedTail = this.tail;
|
const deletedTail = this.tail;
|
||||||
this.head = null;
|
this.head = null;
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
@ -158,7 +168,9 @@ export default class DoublyLinkedList {
|
|||||||
return deletedTail;
|
return deletedTail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are many nodes in linked list...
|
||||||
const deletedTail = this.tail;
|
const deletedTail = this.tail;
|
||||||
|
|
||||||
this.tail = this.tail.previous;
|
this.tail = this.tail.previous;
|
||||||
this.tail.next = null;
|
this.tail.next = null;
|
||||||
|
|
||||||
|
@ -128,6 +128,7 @@ export default class LinkedList {
|
|||||||
*/
|
*/
|
||||||
deleteTail() {
|
deleteTail() {
|
||||||
if (this.head === this.tail) {
|
if (this.head === this.tail) {
|
||||||
|
// There is only one node in linked list.
|
||||||
const deletedTail = this.tail;
|
const deletedTail = this.tail;
|
||||||
this.head = null;
|
this.head = null;
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
@ -135,6 +136,7 @@ export default class LinkedList {
|
|||||||
return deletedTail;
|
return deletedTail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are many nodes in linked list...
|
||||||
const deletedTail = this.tail;
|
const deletedTail = this.tail;
|
||||||
|
|
||||||
// Rewind to the last node and delete "next" link for the node before the last one.
|
// Rewind to the last node and delete "next" link for the node before the last one.
|
||||||
@ -148,6 +150,7 @@ export default class LinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.tail = currentNode;
|
this.tail = currentNode;
|
||||||
|
|
||||||
return deletedTail;
|
return deletedTail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user