Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new node at the given position in the linked list.
DSA C#
public void InsertAtPosition(int data, int position) {
Node newNode = new Node(data);
if (position == 1) {
newNode.Next = head;
head = [1];
return;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to update the head pointer.
Setting head to null instead of the new node.
✗ Incorrect
When inserting at position 1, the new node becomes the head of the list.
2fill in blank
mediumComplete the code to move to the node just before the insertion position.
DSA C#
Node current = head; for (int i = 1; i < [1] - 1; i++) { current = current.Next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using data instead of position in the loop.
Looping until position instead of position - 1.
✗ Incorrect
We loop until the node before the desired position to insert the new node.
3fill in blank
hardFix the error in linking the new node to the next node.
DSA C#
newNode.Next = [1].Next;
current.Next = newNode;Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.Next to newNode itself.
Using head or position instead of current.
✗ Incorrect
The new node should point to the next node after current.
4fill in blank
hardFill both blanks to complete the insertion logic inside the method.
DSA C#
public void InsertAtPosition(int data, int position) {
Node newNode = new Node(data);
if (position == 1) {
newNode.Next = head;
head = [1];
return;
}
Node current = head;
for (int i = 1; i < [2] - 1; i++) {
current = current.Next;
}
newNode.Next = current.Next;
current.Next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not updating head when position is 1.
Looping incorrectly with wrong bounds.
✗ Incorrect
At position 1, head is updated to newNode. The loop runs until position - 1 to find the insertion point.
5fill in blank
hardFill all three blanks to create a method that inserts a node at a specific position in a singly linked list.
DSA C#
public void InsertAtPosition(int data, int position) {
Node newNode = new Node(data);
if (position == 1) {
newNode.Next = [1];
[2] = newNode;
return;
}
Node current = head;
for (int i = 1; i < [3] - 1; i++) {
current = current.Next;
}
newNode.Next = current.Next;
current.Next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Confusing newNode and head in assignments.
Incorrect loop bounds causing wrong insertion.
✗ Incorrect
newNode.Next points to head, then head is updated to newNode. The loop runs until position - 1 to find the insertion point.
