Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the head node of a singly linked list.
DSA Java
if (head == null) return null; head = head[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because singly linked lists don't have 'prev'.
Trying to assign head to 'child' or 'last' which are not valid references.
✗ Incorrect
To delete the head node, we move the head pointer to the next node.
2fill in blank
mediumComplete the code to traverse the list to the node before the one to delete at position pos.
DSA Java
Node current = head; for (int i = 1; i < [1] - 1 && current != null; i++) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'size' instead of the position variable causes incorrect traversal.
Looping until 'pos' instead of 'pos - 1' skips the correct node.
✗ Incorrect
We traverse until the node before the position to delete, so we use 'pos'.
3fill in blank
hardFix the error in deleting the node at position pos by correctly updating the next pointer.
DSA Java
if (current == null || current.next == null) return head; current.next = current.next[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'child' causes errors because those references don't exist in singly linked lists.
Not updating the pointer correctly leads to memory leaks or incorrect list structure.
✗ Incorrect
To delete the node at position pos, link current node to the node after the next one using 'next'.
4fill in blank
hardFill both blanks to complete the method that deletes a node at position pos in a singly linked list.
DSA Java
public Node deleteAtPosition(Node head, int pos) {
if (head == null) return null;
if (pos == 1) {
head = head[1];
return head;
}
Node current = head;
for (int i = 1; i < pos - 1 && current != null; i++) {
current = current[2];
}
if (current == null || current.next == null) return head;
current.next = current.next.next;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'child' instead of 'next' causes errors.
Not updating pointers correctly leads to broken list.
✗ Incorrect
To delete the head, move head to head.next. To traverse, move current to current.next.
5fill in blank
hardFill all three blanks to complete the method that deletes a node at position pos and returns the updated list.
DSA Java
public Node deleteAtPosition(Node head, int pos) {
if (head == null) return null;
if (pos == 1) {
head = head[1];
return head;
}
Node current = head;
for (int i = 1; i < pos - 1 && current != null; i++) {
current = current[2];
}
if (current == null || current.next == null) return head;
current.next = current.next[3];
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' causes errors because singly linked lists don't have backward links.
Not updating pointers correctly breaks the list.
✗ Incorrect
All blanks require 'next' to move forward or skip nodes in the singly linked list.
