Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA C#
if (head == [1]) { return null; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head equals head (always true).
Using 'empty' which is not a keyword.
✗ Incorrect
We check if head is null to know if the list is empty before deleting a node.
2fill in blank
mediumComplete the code to find the node before the head in the circular linked list.
DSA C#
Node current = head; while (current.next != [1]) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking current.next != null which never happens in circular list.
Using an undefined variable like tail.
✗ Incorrect
In a circular linked list, the last node points back to head, so we check current.next != head.
3fill in blank
hardFix the error in the code to delete the head node correctly.
DSA C#
if (head.data == key) { if (head.next == head) { head = [1]; } else { Node last = head; while (last.next != head) { last = last.next; } last.next = head.next; head = head.next; } return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to head.next which causes a loop.
Using 'this' which is invalid here.
✗ Incorrect
When the list has only one node and it matches the key, we set head to null to delete it.
4fill in blank
hardFill both blanks to delete a node with given key that is not the head.
DSA C#
Node current = head; while (current.next != head && current.next.data != [1]) { current = current.next; } if (current.next.data == [2]) { current.next = current.next.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using head or current instead of key for data comparison.
Not checking if node with key exists before deletion.
✗ Incorrect
We look for the node whose next node has the key, then skip it by changing next pointer.
5fill in blank
hardFill all three blanks to complete the delete function for circular linked list.
DSA C#
public Node DeleteNode(Node head, int key) {
if (head == [1]) return null;
Node current = head;
if (head.data == key) {
if (head.next == head) {
head = [2];
} else {
Node last = head;
while (last.next != head) {
last = last.next;
}
last.next = head.next;
head = [3].next;
}
return head;
}
while (current.next != head && current.next.data != key) {
current = current.next;
}
if (current.next.data == key) {
current.next = current.next.next;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of null.
Not updating head after deleting the first node.
✗ Incorrect
We check if head is null, set head to null if only one node is deleted, and update head to next node after deletion.
