Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node and insert it at the beginning of the linked list.
DSA Java
Node newNode = new Node(data);
newNode.next = [1];
head = newNode;Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null loses the rest of the list.
Assigning newNode.next to newNode creates a cycle.
✗ Incorrect
The new node's next should point to the current head before updating head to newNode.
2fill in blank
mediumComplete the method to insert a new node at the beginning of the linked list.
DSA Java
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = [1];
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null loses the rest of the list.
Assigning newNode.next to newNode creates a cycle.
✗ Incorrect
The new node's next should point to the current head before updating head to newNode.
3fill in blank
hardFix the error in the insertAtBeginning method to correctly insert a node at the head.
DSA Java
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = [1];
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to newNode causes a cycle.
Setting newNode.next to null loses the rest of the list.
✗ Incorrect
The new node's next must point to the current head to keep the list intact.
4fill in blank
hardFill both blanks to create a method that inserts a new node at the beginning and updates the head.
DSA Java
public void insertAtBeginning(int data) {
Node newNode = new Node([1]);
newNode.next = [2];
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using newNode instead of data when creating the node.
Setting newNode.next to null loses the rest of the list.
✗ Incorrect
The new node is created with data, and its next points to the current head before updating head.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning method that creates a new node, links it, and updates the head.
DSA Java
public void insertAtBeginning(int [1]) { Node newNode = new Node([2]); newNode.next = [3]; head = newNode; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using newNode as method parameter name.
Setting newNode.next to newNode causes a cycle.
✗ Incorrect
The method parameter is data, used to create the new node, whose next points to head before updating head.
