Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given data.
DSA Java
Node newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of data when creating the node.
Passing 'null' or 'head' instead of the data value.
✗ Incorrect
We pass the data value to the Node constructor to create a new node with that data.
2fill in blank
mediumComplete the code to check if the list is empty before inserting.
DSA Java
if (head == [1]) { head = newNode; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head equals newNode instead of null.
Using 'data' or 'head' in the condition incorrectly.
✗ Incorrect
If head is null, the list is empty, so we set head to the new node.
3fill in blank
hardFix the error in the loop that finds the last node.
DSA Java
Node temp = head; while (temp.[1] != null) { temp = temp.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' or 'head' instead of 'next' in the loop condition.
Using 'prev' which is not defined in singly linked list.
✗ Incorrect
We check temp.next to find the last node, so the loop continues while temp.next is not null.
4fill in blank
hardFill both blanks to link the new node at the end of the list.
DSA Java
temp.[1] = newNode; newNode.[2] = null;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' or 'prev' instead of 'next' for linking nodes.
Not setting newNode.next to null.
✗ Incorrect
We set temp.next to newNode to link it at the end, and newNode.next to null as it is the last node.
5fill in blank
hardFill all three blanks to complete the insertAtEnd method.
DSA Java
public void insertAtEnd(int data) {
Node newNode = new Node([1]);
if (head == [2]) {
head = newNode;
return;
}
Node temp = head;
while (temp.[3] != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if head is null before traversal.
Using wrong variable names in conditions or loop.
Not setting newNode.next to null.
✗ Incorrect
We create newNode with data, check if head is null (empty list), then traverse using next until last node to insert newNode.
