Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given data.
DSA Java
Node newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using head or tail instead of data.
Passing null instead of data.
✗ 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.
DSA Java
if ([1] == null) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking newNode instead of head.
Checking tail instead of head.
✗ Incorrect
If head is null, the list is empty.
3fill in blank
hardFix the error in linking the new node at the end.
DSA Java
tail.[1] = newNode; newNode.[2] = tail; tail = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping next and prev pointers.
Not updating tail pointer.
✗ Incorrect
The current tail's next should point to newNode, and newNode's prev should point to tail.
4fill in blank
hardFill both blanks to update head and tail when list is empty.
DSA Java
if (head == null) { head = [1]; tail = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to tail or vice versa instead of newNode.
Assigning null instead of newNode.
✗ Incorrect
When the list is empty, both head and tail point to the new 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 == null) {
head = [2];
tail = [3];
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables in assignments.
Not linking nodes correctly.
✗ Incorrect
Create newNode with data, assign head and tail to newNode if empty, else link at end.
