What is the first step when deleting a node at a specific position in a singly linked list?
Check if the list is empty or if the position is valid (e.g., position is not negative and within the list length).
Click to reveal answer
beginner
How do you delete the head node (position 0) in a singly linked list?
Make the head pointer point to the next node of the current head, effectively removing the first node.
Click to reveal answer
intermediate
Why do you need to keep track of the previous node when deleting a node at a specific position (other than head)?
Because you need to update the previous node's next pointer to skip the node being deleted and link to the next node after it.
Click to reveal answer
beginner
What happens if you try to delete a node at a position greater than the length of the linked list?
No deletion occurs because the position is invalid; the list remains unchanged.
Click to reveal answer
beginner
In TypeScript, what type should the 'next' property of a linked list node have?
It should be either another node of the same type or null if it is the last node.
Click to reveal answer
What should you do first when deleting a node at position 3 in a singly linked list?
ADelete the head node
BTraverse the list to the node at position 2
CSet the head to null
DTraverse the list to the node at position 4
✗ Incorrect
To delete the node at position 3, you need to reach the node just before it, which is at position 2.
If the linked list is empty, what happens when you try to delete a node at any position?
AAn error occurs
BThe head becomes undefined
CA new node is added
DThe list remains empty with no changes
✗ Incorrect
If the list is empty, there is nothing to delete, so the list stays empty.
After deleting a node at position 1, what should the previous node's next pointer point to?
ANull
BThe node at position 1
CThe node at position 2
DThe head node
✗ Incorrect
The previous node's next pointer should skip the deleted node and point to the node after it.
What is the time complexity of deleting a node at a specific position in a singly linked list?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
You may need to traverse up to n nodes to reach the position, so the time complexity is O(n).
Which of these is NOT needed when deleting a node at a specific position?
APointer to the tail node
BPointer to the node to delete
CPointer to the next node after the deleted node
DPointer to the previous node
✗ Incorrect
The tail pointer is not required to delete a node at a specific position.
Explain the steps to delete a node at a specific position in a singly linked list.
Think about how you move through the list and change links.
You got /5 concepts.
Describe how you would implement node deletion at a specific position in TypeScript for a singly linked list.
Focus on pointer updates and boundary checks.
You got /5 concepts.
Practice
(1/5)
1. What does deleting a node at a specific position in a singly linked list mean?
easy
A. Removing the node located at that position and linking its previous node to the next node
B. Adding a new node at that position
C. Swapping the node at that position with the last node
D. Counting the total nodes in the list
Solution
Step 1: Understand node deletion in linked list
Deleting a node means removing it and connecting the previous node to the next node to keep the list intact.
Step 2: Apply to specific position
At a specific position, we find the node before it and change its next pointer to skip the node to delete.
Final Answer:
Removing the node located at that position and linking its previous node to the next node -> Option A
Quick Check:
Delete node = remove and link neighbors [OK]
Hint: Delete means remove and link neighbors, not add or swap [OK]
Common Mistakes:
Thinking deletion adds a node
Confusing deletion with swapping nodes
Ignoring the need to link previous and next nodes
2. Which of the following TypeScript code snippets correctly deletes a node at position pos in a singly linked list starting with head?
easy
A. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos - 1; i++) temp = temp.next; temp.next = temp.next.next; }
B. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos; i++) temp = temp.next; temp.next = temp.next.next; }
C. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i <= pos; i++) temp = temp.next; temp.next = temp.next.next; }
D. if (pos === 0) head = head.next; else { let temp = head; for (let i = 1; i <= pos; i++) temp = temp.next; temp.next = temp.next.next; }
Solution
Step 1: Handle deletion at head (position 0)
If position is 0, simply move head to next node to delete the first node.
Step 2: Traverse to node before position
Loop from 0 to pos-1 to reach node before the one to delete, then link its next to next.next.
Final Answer:
if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos - 1; i++) temp = temp.next; temp.next = temp.next.next; } -> Option A
Quick Check:
Loop to pos-1, then skip node [OK]
Hint: Loop to pos-1 to delete node at pos [OK]
Common Mistakes:
Looping to pos instead of pos-1
Not handling deletion at head correctly
Accessing temp.next.next when temp.next is null
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null and the code below, what will be the list after deleting the node at position 2?
let head = { val: 10, next: { val: 20, next: { val: 30, next: { val: 40, next: { val: 50, next: null } } } } };
let pos = 2;
if (pos === 0) head = head.next;
else {
let temp = head;
for (let i = 0; i < pos - 1; i++) temp = temp.next;
temp.next = temp.next.next;
}
medium
A. 10 -> 30 -> 40 -> 50 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> null
C. 10 -> 20 -> 40 -> 50 -> null
D. 20 -> 30 -> 40 -> 50 -> null
Solution
Step 1: Identify node at position 2
Position 2 node has value 30 (0-based indexing: 10 at 0, 20 at 1, 30 at 2).
Step 2: Delete node at position 2
Traverse to node at position 1 (value 20), then set its next to node at position 3 (value 40), skipping 30.
Final Answer:
10 -> 20 -> 40 -> 50 -> null -> Option C
Quick Check:
Node 30 removed, neighbors linked [OK]
Hint: Skip node at pos by linking previous to next.next [OK]
Common Mistakes:
Deleting wrong node due to off-by-one error
Not updating links properly
Confusing position indexing
4. The following TypeScript code attempts to delete a node at position pos in a singly linked list but causes a runtime error. What is the error?
function deleteNode(head: Node | null, pos: number): Node | null {
if (pos === 0) return head.next;
let temp = head;
for (let i = 0; i < pos; i++) {
temp = temp.next!;
}
temp.next = temp.next!.next;
return head;
}
medium
A. Incorrect loop range causes skipping the wrong node
B. Accessing temp.next when temp is null causes runtime error
C. Missing check for empty list before deletion
D. Returning head.next instead of head after deletion
Solution
Step 1: Analyze loop traversal
The loop runs from 0 to pos, moving temp forward pos times, which can make temp null if pos >= list length.
Step 2: Identify null access
After loop, temp may be null, so accessing temp.next causes runtime error.
Final Answer:
Accessing temp.next when temp is null causes runtime error -> Option B
Quick Check:
Loop overshoots, temp becomes null, then temp.next fails [OK]
Hint: Loop to pos-1, not pos, to avoid null access [OK]
Common Mistakes:
Looping too far causing null pointer
Not checking for empty list
Misunderstanding return value after deletion
5. You have a singly linked list representing a playlist: Song1 -> Song2 -> Song3 -> Song4 -> Song5 -> null. You want to delete the 4th song (position 3). After deletion, you want to insert a new song Song6 at the same position. Which sequence of operations correctly achieves this?
hard
A. Delete node at position 3, then insert new node at head
B. Delete node at position 3, then traverse to position 3 and insert new node after it
C. Delete node at position 2, then traverse to position 3 and insert new node after it
D. Delete node at position 3, then traverse to position 2 and insert new node after it
Solution
Step 1: Delete node at position 3
Remove the node at position 3 (Song4) by linking node at position 2 to node at position 4.
Step 2: Insert new node at position 3
Traverse to position 2, then insert new node after it to place Song6 at position 3.
Final Answer:
Delete node at position 3, then traverse to position 2 and insert new node after it -> Option D
Quick Check:
Delete at pos 3, insert after pos 2 = new pos 3 [OK]
Hint: Insert after pos-1 to place new node at pos [OK]
Common Mistakes:
Inserting at wrong position after deletion
Deleting wrong node position
Inserting at head instead of correct position
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.