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' instead of 'data' to create the new node.
Passing 'null' or 'next' which are not data values.
✗ Incorrect
We create a new node by passing the data value to the Node constructor.
2fill in blank
mediumComplete the code to link the new node's next pointer to the current head.
DSA Java
newNode.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to itself or null.
Confusing prev and next pointers.
✗ Incorrect
The new node's next should point to the current head node.
3fill in blank
hardFix the error in setting the previous pointer of the old head node.
DSA Java
if (head != null) { head.[1] = newNode; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.next instead of head.prev.
Trying to set head.data or head itself.
✗ Incorrect
The old head's previous pointer should point back to the new node.
4fill in blank
hardFill both blanks to update the head pointer and the new node's previous pointer.
DSA Java
head = [1]; [2].prev = null;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.prev to null instead of newNode.prev.
Using head instead of newNode in the second line.
✗ Incorrect
The head pointer is updated to the new node, and the new node's previous pointer is set to null because it is now the first node.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning method for a doubly linked list.
DSA Java
public void insertAtBeginning(int data) {
Node newNode = new Node([1]);
newNode.next = [2];
if (head != null) {
head.prev = newNode;
}
head = [3];
newNode.prev = null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up newNode and head in assignments.
Forgetting to set newNode.prev to null.
✗ Incorrect
We create a new node with data, link its next to the current head, update the old head's prev pointer, then update head to newNode and set newNode.prev to null.
