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 Java
Node temp = current.[1];
current.next = current.prev;
current.prev = temp;Attempts:
3 left
💡 Hint
Common Mistakes
Swapping data instead of pointers
Not saving the pointer before overwriting it
✗ Incorrect
We need to store the current node's next pointer in temp before swapping it with prev.
2fill in blank
mediumComplete the code to move to the next node after swapping pointers.
DSA Java
current = current.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next which now points backward
Using a wrong pointer like head or temp
✗ Incorrect
After swapping, the original next node is now in prev, so move to current.prev.
3fill in blank
hardFix the error in updating the head pointer after reversal.
DSA Java
if (temp != null) { head = temp.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to temp.next which is null
Setting head to temp.data instead of a node
✗ Incorrect
After the loop, temp points to the old head, so head should be updated to temp.prev which is the new head.
4fill in blank
hardFill both blanks to complete the reversal loop and update head correctly.
DSA Java
while (current != null) { Node [1] = current.next; current.next = current.prev; current.prev = [2]; current = current.prev; } if (temp != null) { head = temp.prev; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current instead of temp for storing next
Mixing up the order of assignments
✗ Incorrect
We use temp to store current.next before swapping, then assign current.prev to temp after swapping.
5fill in blank
hardFill all three blanks to complete the full reverse method for a doubly linked list.
DSA Java
public Node reverse(Node head) {
Node current = head;
Node [1] = null;
while (current != null) {
[2] = current.next;
current.next = current.prev;
current.prev = [3];
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not declaring temp or using wrong variable names
Swapping pointers in wrong order
✗ Incorrect
Declare temp as null, assign temp = current.next before swapping, and assign current.prev = temp after swapping.
