Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the last node from the linked list.
DSA Javascript
pop() {
if (!this.head) return null;
if (!this.head.next) {
const poppedNode = this.head;
this.head = null;
return poppedNode;
}
let current = this.head;
while (current.next.next) {
current = current.[1];
}
const poppedNode = current.next;
current.next = null;
return poppedNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Trying to access 'head' or 'tail' properties inside the loop.
✗ Incorrect
To reach the second last node, we move current to current.next repeatedly until current.next.next is null.
2fill in blank
mediumComplete the code to update the linked list after popping the last node.
DSA Javascript
pop() {
if (!this.head) return null;
if (!this.head.next) {
const poppedNode = this.head;
this.head = null;
return poppedNode;
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
const poppedNode = current.next;
current.[1] = null;
return poppedNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting current.prev to null instead of current.next.
Trying to set head or tail to null incorrectly.
✗ Incorrect
Setting current.next to null removes the last node from the list.
3fill in blank
hardFix the error in the pop method to correctly handle the empty list case.
DSA Javascript
pop() {
if (this.head === [1]) return null;
// rest of the code
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Checking for 0 or false which are not valid for head.
✗ Incorrect
The head is null when the list is empty, so we check if this.head === null.
4fill in blank
hardFill both blanks to complete the pop method that returns the popped node's value.
DSA Javascript
pop() {
if (!this.head) return null;
if (!this.head.next) {
const poppedNode = this.head;
this.head = null;
return poppedNode.[1];
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
const poppedNode = current.next;
current.[2] = null;
return poppedNode.value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the node instead of its value.
Setting current.prev to null instead of current.next.
✗ Incorrect
Return the value of the popped node and set current.next to null to remove it.
5fill in blank
hardFill all three blanks to complete the pop method that updates tail and returns the popped node.
DSA Javascript
pop() {
if (!this.head) return null;
if (!this.head.next) {
const poppedNode = this.head;
this.head = null;
this.[1] = null;
return poppedNode;
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
const poppedNode = current.next;
current.next = null;
this.[2] = current;
return poppedNode;
// this.[3] is updated to the new tail
}Attempts:
3 left
💡 Hint
Common Mistakes
Updating head instead of tail.
Not updating tail after popping.
✗ Incorrect
The tail pointer must be updated to current after popping the last node.
