Delete Node at Specific Position in DSA Typescript - Time & Space Complexity
We want to know how long it takes to remove a node from a list at a certain spot.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
function deleteNode(head: ListNode | null, position: number): ListNode | null {
if (!head) return null;
if (position === 0) return head.next;
let current = head;
for (let i = 0; i < position - 1; i++) {
if (!current.next) return head;
current = current.next;
}
if (current.next) current.next = current.next.next;
return head;
}
This code removes the node at the given position from a linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list nodes.
- How many times: Up to position - 1 times, depending on where the node is.
As the list gets longer, the time to reach the position grows roughly the same as the position number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach the node |
| 100 | Up to 99 steps to reach the node |
| 1000 | Up to 999 steps to reach the node |
Pattern observation: The time grows linearly with the position in the list.
Time Complexity: O(n)
This means the time to delete a node grows in direct proportion to the position of the node.
[X] Wrong: "Deleting a node always takes the same time no matter where it is."
[OK] Correct: You must first find the node, which takes longer if it is farther in the list.
Understanding how time grows with list size helps you explain your code clearly and shows you know how data structures work.
"What if we had a pointer directly to the node before the one to delete? How would the time complexity change?"
Practice
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 AQuick Check:
Delete node = remove and link neighbors [OK]
- Thinking deletion adds a node
- Confusing deletion with swapping nodes
- Ignoring the need to link previous and next nodes
pos in a singly linked list starting with head?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 AQuick Check:
Loop to pos-1, then skip node [OK]
- Looping to pos instead of pos-1
- Not handling deletion at head correctly
- Accessing temp.next.next when temp.next is null
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;
}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 CQuick Check:
Node 30 removed, neighbors linked [OK]
- Deleting wrong node due to off-by-one error
- Not updating links properly
- Confusing position indexing
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;
}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 BQuick Check:
Loop overshoots, temp becomes null, then temp.next fails [OK]
- Looping too far causing null pointer
- Not checking for empty list
- Misunderstanding return value after deletion
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?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 DQuick Check:
Delete at pos 3, insert after pos 2 = new pos 3 [OK]
- Inserting at wrong position after deletion
- Deleting wrong node position
- Inserting at head instead of correct position
Ready to prove it?
Pass the test and this pattern moves unknown
