There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I don’t understand the complexity in the removeHead and removeTail when it comes to the linked list consisting of one node. The CA way has removeHead calling removeTail or removeTail calling removeHead in these cases. If you call removeHead() which calls removeTail(), removeTail() simply returns the data property which is the same as in the caller function. See the code below.
But if you step thru the code, it seems as if those are they unnecessary function calls. It is easy to check if there is only one node because this.head === this.tail. If calling removeHead, set next to null. If calling removeTail, set previous to null.
//case 1: There is a next node that is not the tail (trivial)
//case 2: There is a next node that is the tail
//case 3: The head is the only node
//case 4: There is no node (trivial)
//If a list has just one node, that node is both the head and tail of the list
//so if head is null or tail is null, means the list is empty
removeHead() {
const removedHead = this.head;
if (!removedHead) { //case 4
return;
}
//The next node in the list will become the head.
this.head = removedHead.getNextNode(); //case 2: this.head points to the tail,
//case 3: There only a head node, removedHead.getNextNode() returns null
if (this.head) { // case 2: after this, the tail node is the head and tail.next, and tail.previous equal null,
// case 3: does not run
this.head.setPreviousNode(null);
}
if (removedHead === this.tail) { //case 2: removedHead !== tail, so does not run
//case 3: removeHead === tail, so call removeTail
this.removeTail();
}
return removedHead.data; // only case 4 does not apply here
}
//If called from removeHead() this.tail equals whatever this.tail was in removeHead().
//Recall all this is happeining in the same instance and there is only one this.tail.
//When the below code runs this line (this.tail = removedTail.getPreviousNode();),
//this.tail will be set to null but removedTail has the original value of this.tail
removeTail() {
const removedTail = this.tail;
if (!removedTail) {
return;
}
//the previous node will become the tail, if there is one
this.tail = removedTail.getPreviousNode(); // if case 3, then this.tail === null
if (this.tail) {// if case 3, this will not run
this.tail.setNextNode(null);
}
if (removedTail === this.head) { // case 3:
this.removeHead();
}
return removedTail.data;
}