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 C#
current.[1] = temp;Attempts:
3 left
💡 Hint
Common Mistakes
Swapping data instead of pointers
Assigning to current.next instead of current.prev
✗ Incorrect
We swap the next and prev pointers. Here, current.prev is assigned temp which holds the original next node.
2fill in blank
mediumComplete the code to move to the next node in the original list during reversal.
DSA C#
current = current.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next which now points backwards
Using head or tail instead of pointers
✗ Incorrect
After swapping, the original next node is now in current.prev, so we move current to current.prev.
3fill in blank
hardFix the error in updating the head pointer after the reversal loop.
DSA C#
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 pointer
✗ Incorrect
After the loop, temp points to the last processed node, so head should be updated to temp.prev which is the new head.
4fill in blank
hardFill both blanks to correctly swap pointers and move current in the reversal loop.
DSA C#
Node temp = current.[1]; current.[2] = current.[1]; current.[1] = temp; current = current.[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up next and prev assignments
Not moving current correctly after swapping
✗ Incorrect
We first save current.next in temp, then assign current.next to current.prev, then current.prev to temp, and finally move current to current.prev (which is saved in current.next before swap).
5fill in blank
hardFill all three blanks to complete the reversal function for a doubly linked list.
DSA C#
public Node Reverse(Node head) {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.[1];
current.[2] = current.[3];
current.[3] = temp;
current = current.[2];
}
if (temp != null) head = temp.[1];
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Confusing next and prev in assignments
Not updating head correctly after reversal
✗ Incorrect
Inside the loop, temp saves current.next, then current.prev is assigned current.next, current.next is assigned temp, and current moves to current.prev. After the loop, head is updated to temp.prev which is the new head.
