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 an undefined variable name instead of the parameter 'data'.
Trying to create a node without passing any value.
✗ Incorrect
We create a new node by passing the data value to the Node constructor.
2fill in blank
mediumComplete the code to check if the list is empty (head is null).
DSA C#
if (head [1] null) { head = newNode; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison '=='.
Using '!=' which checks for not null.
✗ Incorrect
We check if head is equal to null to know if the list is empty.
3fill in blank
hardFix the error in the loop that finds the last node.
DSA C#
Node last = head; while (last.[1] != null) { last = last.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes infinite loop or error.
Using 'head' or 'last' as property names incorrectly.
✗ Incorrect
To find the last node, we follow the 'next' pointers until null.
4fill in blank
hardFill both blanks to link the new node at the end of the list.
DSA C#
last.next = [1]; [2].prev = last;
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'last' and 'newNode' in assignments.
Not setting the prev pointer of the new node.
✗ Incorrect
We set last.next to newNode and newNode.prev to last to link the new node.
5fill in blank
hardFill all three blanks to complete the InsertAtEnd method.
DSA C#
public void InsertAtEnd(int data) {
Node newNode = new Node([1]);
if (head == null) {
head = [2];
return;
}
Node last = head;
while (last.[3] != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in assignments.
Using 'prev' instead of 'next' in the while loop condition.
✗ Incorrect
We create newNode with data, assign head to newNode if list empty, and traverse using next.
