Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the next and prev pointers of the current node.
DSA Javascript
function reverseDLL(head) {
let current = head;
while (current !== null) {
let temp = current.next;
current.next = [1];
current.prev = temp;
current = temp;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current.next to current.next instead of current.prev.
Not swapping pointers correctly.
✗ Incorrect
We swap the next and prev pointers by assigning current.prev to current.next.
2fill in blank
mediumComplete the code to update the head pointer after reversing the list.
DSA Javascript
function reverseDLL(head) {
let current = head;
let newHead = null;
while (current !== null) {
let temp = current.next;
current.next = current.prev;
current.prev = temp;
newHead = current;
current = temp;
}
return [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the old head instead of the new head.
Returning current or temp which may be null.
✗ Incorrect
After reversing, newHead points to the last node processed, which is the new head.
3fill in blank
hardFix the error in the loop condition to correctly traverse the list.
DSA Javascript
function reverseDLL(head) {
let current = head;
while ([1]) {
let temp = current.next;
current.next = current.prev;
current.prev = temp;
current = temp;
}
return current;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current === null which stops the loop immediately.
Using head or temp in the condition incorrectly.
✗ Incorrect
The loop should continue while current is not null to traverse all nodes.
4fill in blank
hardFill both blanks to correctly swap pointers and update the current node.
DSA Javascript
function reverseDLL(head) {
let current = head;
let newHead = null;
while (current !== null) {
let temp = current.[1];
current.[2] = current.prev;
current.prev = temp;
newHead = current;
current = temp;
}
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current.prev instead of current.next for temp.
Assigning current.prev to current.next instead of the other way.
✗ Incorrect
We store current.next in temp and assign current.prev to current.next to swap pointers.
5fill in blank
hardFill all three blanks to complete the reverse function with correct pointer swaps and return.
DSA Javascript
function reverseDLL(head) {
let current = head;
let newHead = null;
while (current !== null) {
let temp = current.[1];
current.[2] = current.[3];
current.prev = temp;
newHead = current;
current = temp;
}
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up next and prev in assignments.
Not saving current.next before swapping.
✗ Incorrect
We save current.next in temp, assign current.prev to current.next, and current.prev to temp to swap pointers.
