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 Typescript
function insertAtPosition(head: ListNode | null, val: number, pos: number): ListNode | null {
const newNode = new ListNode(val);
if (pos === 0) {
newNode.next = head;
return [1];
}
let current = head;
let count = 0;
while (current !== null && count < pos - 1) {
current = current.next;
count++;
}
if (current === null) return head;
newNode.next = current.next;
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the old head instead of the new node when inserting at position 0.
Not updating the new node's next pointer.
✗ Incorrect
When inserting at position 0, the new node becomes the new head, so we return newNode.
2fill in blank
mediumComplete the code to traverse the list until the node before the insertion position.
DSA Typescript
let current = head; let count = 0; while (current !== null && [1] < pos - 1) { current = current.next; count++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current' instead of 'count' in the loop condition.
Using 'pos' directly in the loop condition.
✗ Incorrect
We use 'count' to track how many nodes we have traversed until reaching the node before the insertion position.
3fill in blank
hardFix the error in the code that inserts the new node after the current node.
DSA Typescript
newNode.next = [1].next; [2].next = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'newNode' instead of 'current' in these assignments.
Not updating both pointers correctly.
✗ Incorrect
We must use 'current' to access the next node and update its next pointer to the new node.
4fill in blank
hardFill both blanks to handle the case when the insertion position is beyond the end of the list (do not insert).
DSA Typescript
if (current === null) { [1] = newNode; return [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning newNode instead of head.
Assigning newNode to head instead of current.
✗ Incorrect
If current is null, it means the position is beyond the list length. Assigning newNode to current has no effect on the list (local variable), and we return head unchanged without inserting.
5fill in blank
hardFill all three blanks to complete the function that inserts a node at a specific position.
DSA Typescript
function insertAtPosition(head: ListNode | null, val: number, pos: number): ListNode | null {
const newNode = new ListNode(val);
if (pos === 0) {
newNode.next = head;
return [1];
}
let current = head;
let count = 0;
while (current !== null && count < [2]) {
current = current.next;
count++;
}
if (current === null) return head;
newNode.next = current.[3];
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect loop condition.
Not updating newNode.next correctly.
Returning wrong node at position 0.
✗ Incorrect
Return newNode when inserting at position 0; loop until count < pos - 1; set newNode.next to current.next.
