Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given data.
DSA C#
Node newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'data' when creating the new node.
Using 'null' which is incorrect for node data.
✗ Incorrect
We create a new node with the given data value to insert it at the beginning.
2fill in blank
mediumComplete the code to handle the case when the list is empty.
DSA C#
if (head == null) { newNode.next = [1]; head = newNode; return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null breaks circularity.
Setting newNode.next to head when head is null causes errors.
✗ Incorrect
For an empty circular list, the new node points to itself to maintain circularity.
3fill in blank
hardFix the error in the loop that finds the last node.
DSA C#
Node temp = head; while (temp.next != [1]) { temp = temp.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for temp.next != null causes infinite loop.
Using newNode or temp in the condition is incorrect.
✗ Incorrect
The last node points back to head in a circular linked list, so we loop until temp.next equals head.
4fill in blank
hardFill both blanks to update pointers to insert the new node at the beginning.
DSA C#
temp.next = [1]; newNode.next = [2]; head = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting temp.next to head instead of newNode.
Setting newNode.next to temp instead of head.
✗ Incorrect
The last node's next points to newNode, newNode's next points to old head, and head is updated to newNode.
5fill in blank
hardFill all three blanks to complete the InsertAtBeginning method for a circular linked list.
DSA C#
public Node InsertAtBeginning(Node head, int data) {
Node newNode = new Node([1]);
if (head == null) {
newNode.next = [2];
head = newNode;
return head;
}
Node temp = head;
while (temp.next != [3]) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
head = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using null instead of newNode for newNode.next in empty list.
Loop condition checking temp.next != null instead of head.
✗ Incorrect
We create newNode with data, set newNode.next to itself if list is empty, and loop until temp.next equals head to find last node.
