Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new node at the given position in a singly linked list.
DSA Javascript
function insertAtPosition(head, data, position) {
let newNode = { value: data, next: null };
if (position === 0) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 0;
while (current !== null && count < position - 1) {
current = current.[1];
count++;
}
if (current === null) return head;
newNode.next = current.next;
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked lists.
Trying to access 'value' to move forward.
Using 'head' inside the loop incorrectly.
✗ Incorrect
To move forward in a singly linked list, we use the 'next' pointer of the current node.
2fill in blank
mediumComplete the code to correctly insert a new node at the middle position of the linked list.
DSA Javascript
function insertMiddle(head, data) {
if (head === null) return { value: data, next: null };
let slow = head;
let fast = head.next;
while (fast !== null && fast.next !== null) {
slow = slow.[1];
fast = fast.next.next;
}
let newNode = { value: data, next: slow.next };
slow.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not defined in singly linked lists.
Trying to move slow pointer using 'value'.
Using 'head' instead of 'next' to move forward.
✗ Incorrect
To move the slow pointer forward, we use the 'next' property of the current node.
3fill in blank
hardFix the error in the code to insert a node at a specific position in the linked list.
DSA Javascript
function insertAtPos(head, data, pos) {
let newNode = { value: data, next: null };
if (pos === 0) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 0;
while (current !== null && count < pos - 1) {
current = current.next;
count++;
}
if (current === null) return head;
newNode.next = current.[1];
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist.
Using 'value' instead of 'next'.
Using 'head' incorrectly.
✗ Incorrect
To insert the new node, we set its 'next' to the current node's 'next'.
4fill in blank
hardFill both blanks to create a function that inserts a node at a given position in a linked list.
DSA Javascript
function insertNode(head, data, pos) {
let newNode = { value: data, next: null };
if (pos === 0) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 0;
while (current !== null && count < pos - 1) {
current = current.[1];
count[2];
}
if (current === null) return head;
newNode.next = current.next;
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not defined.
Using '--' which decreases count incorrectly.
✗ Incorrect
To move forward, use 'next'. To increase count, use '++'.
5fill in blank
hardFill all three blanks to insert a node at a specific position in a linked list.
DSA Javascript
function insertAt(head, data, pos) {
let newNode = { value: data, next: null };
if (pos === 0) {
newNode.next = head;
return newNode;
}
let current = head;
let count = 0;
while (current !== null && count < pos - 1) {
current = current.[1];
count[2];
}
if (current === null) return head;
newNode.next = current.[3];
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' to decrease count.
Using 'prev' which does not exist.
Setting newNode.next incorrectly.
✗ Incorrect
Move current with 'next', increment count with '++', and set newNode.next to current.next.
