Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new node at the end of a doubly linked list.
DSA C#
public void AddLast(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.[1] = newNode;
newNode.Prev = tail;
tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Prev' instead of 'Next' causes the list to link backwards incorrectly.
Assigning to 'Head' or 'Tail' inside the else block incorrectly.
✗ Incorrect
In a doubly linked list, the 'Next' pointer of the current tail node should point to the new node to add it at the end.
2fill in blank
mediumComplete the code to remove the last node from a doubly linked list.
DSA C#
public void RemoveLast() {
if (tail == null) return;
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.[1];
tail.Next = null;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Next' moves tail forward, which is incorrect.
Setting tail to head or null incorrectly.
✗ Incorrect
To remove the last node, move the tail pointer to the previous node using 'Prev'.
3fill in blank
hardFix the error in the code to insert a node after a given node in a doubly linked list.
DSA C#
public void InsertAfter(Node node, int value) {
if (node == null) return;
Node newNode = new Node(value);
newNode.Next = node.Next;
newNode.Prev = node;
if (node.Next != null) {
node.Next.[1] = newNode;
}
node.Next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Updating 'Next' instead of 'Prev' causes broken backward links.
Not updating the next node's pointer at all.
✗ Incorrect
When inserting after a node, the next node's 'Prev' pointer must point back to the new node.
4fill in blank
hardFill both blanks to traverse a doubly linked list from tail to head and print values.
DSA C#
public void PrintReverse() {
Node current = tail;
while (current != null) {
Console.WriteLine(current.Value);
current = current.[1];
}
}
// To start from tail, assign tail to [2] before loop.Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Next' to move backward causes infinite loop or error.
Starting from 'head' instead of 'tail' for reverse traversal.
✗ Incorrect
To move backward, use 'Prev'. To start from the end, assign 'tail' to the current node.
5fill in blank
hardFill all three blanks to create a dictionary of node values and their previous node values (or null if none).
DSA C#
var prevValues = new Dictionary<int, int?>(); Node current = head; while (current != null) { prevValues[[1]] = current.[2] == null ? (int?)null : current.Prev.Value; current = current.[3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Next' instead of 'Prev' to get previous node value.
Using 'Prev' to move forward instead of 'Next'.
Using 'current' instead of 'current.Value' as dictionary key.
✗ Incorrect
Use 'current.Value' as key, 'Prev' to check previous node, and 'Next' to move forward.
