Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Node class's next pointer.
DSA Java
class Node { int data; Node [1]; Node(int data) { this.data = data; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using incorrect variable names like 'pointer' or 'link'.
✗ Incorrect
The 'next' pointer in a singly linked list node points to the next node.
2fill in blank
mediumComplete the code to initialize the head of the linked list.
DSA Java
class LinkedList { Node head; LinkedList() { this.[1] = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing 'tail' instead of 'head'.
Using other names like 'start' or 'root' which are not standard here.
✗ Incorrect
The head pointer points to the first node in the linked list and is initialized to null when the list is empty.
3fill in blank
hardFix the error in the method to add a new node at the front of the list.
DSA Java
void addFront(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.
Using 'tail' or 'newNode' instead of 'head' causes errors.
✗ Incorrect
To add a node at the front, newNode.next should point to the current head.
4fill in blank
hardFill both blanks to complete the method that prints all node data in the list.
DSA Java
void printList() {
Node current = head;
while (current [1] null) {
System.out.print(current.data + " -> ");
current = current.[2];
}
System.out.println("null");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the while condition.
Using 'prev' instead of 'next' to move forward.
✗ Incorrect
The loop continues while current is not null, and current moves to current.next each iteration.
5fill in blank
hardFill all three blanks to complete the method that counts nodes in the list.
DSA Java
int countNodes() {
int count = 0;
Node current = [1];
while (current [2] null) {
count[3];
current = current.next;
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of head.
Using '--' to decrement count instead of incrementing.
Using '==' instead of '!=' in the loop condition.
✗ Incorrect
Start from head, loop while current is not null, increment count each time.
