Bird
Raised Fist0
DSA Typescriptprogramming~15 mins

Delete Node by Value in DSA Typescript - Deep Dive

Choose your learning style10 modes available
Overview - Delete Node by Value
What is it?
Deleting a node by value means removing the first node in a linked list that contains a specific value. A linked list is a chain of nodes where each node points to the next one. When we delete a node, we change the links so the chain stays connected without the removed node. This operation helps keep the list updated by removing unwanted elements.
Why it matters
Without the ability to delete nodes by value, linked lists would grow endlessly or keep unwanted data, making programs slow or incorrect. This operation allows dynamic data management, like removing a friend from a contact list or deleting a task from a to-do list. It keeps data structures clean and efficient, which is essential for real-world applications.
Where it fits
Before learning this, you should understand what a linked list is and how nodes connect. After this, you can learn about deleting nodes by position, or more complex operations like reversing or sorting linked lists. This topic is a building block for mastering linked list manipulations.
Mental Model
Core Idea
Deleting a node by value means finding the node with that value and changing the previous node's link to skip it, removing it from the chain.
Think of it like...
Imagine a paper chain where each paper ring links to the next. To remove a ring with a certain color, you find it and then connect the ring before it directly to the ring after it, cutting out the unwanted ring.
Head -> [Node1|Val] -> [Node2|Val] -> [Node3|Val] -> null

To delete Node2:
Head -> [Node1|Val] --------> [Node3|Val] -> null
(Node2 is removed)
Build-Up - 7 Steps
1
FoundationUnderstanding Linked List Structure
🤔
Concept: Learn what a linked list is and how nodes connect with pointers.
A linked list is a sequence of nodes. Each node has two parts: a value and a pointer to the next node. The list starts at a head node and ends when a node points to null, meaning no next node.
Result
You can visualize a linked list as a chain of connected nodes, each pointing to the next.
Understanding the basic structure of linked lists is essential before manipulating or deleting nodes.
2
FoundationTraversing a Linked List
🤔
Concept: Learn how to move through each node to find a specific value.
Start at the head node. Check if the current node's value matches the target. If not, move to the next node. Repeat until you find the value or reach the end (null).
Result
You can find whether a value exists in the list and where it is located.
Traversal is the key step before deletion because you must find the node to remove.
3
IntermediateDeleting the Head Node by Value
🤔Before reading on: If the node to delete is the head, do you think you need to change the head pointer or just skip the node?
Concept: Special case: deleting the first node requires updating the head pointer.
If the head node's value matches the target, set the head to point to the next node. This removes the first node from the list.
Result
The head now points to the second node, effectively removing the original head.
Knowing how to handle the head node separately prevents losing access to the list.
4
IntermediateDeleting a Middle or Last Node by Value
🤔Before reading on: To delete a middle node, do you think you change the current node or the previous node's pointer?
Concept: To delete a node other than the head, update the previous node's next pointer to skip the current node.
While traversing, keep track of the previous node. When you find the node to delete, set previous.next = current.next. This skips the current node, removing it from the chain.
Result
The node with the target value is removed, and the list remains connected.
Understanding pointer updates is crucial to maintain list integrity after deletion.
5
IntermediateHandling Value Not Found Cases
🤔
Concept: What happens if the value to delete is not in the list?
Traverse the entire list. If you reach the end without finding the value, do nothing. The list stays unchanged.
Result
No nodes are deleted, and the list remains the same.
Handling this case prevents errors or unintended deletions.
6
AdvancedTypeScript Implementation with Edge Cases
🤔Before reading on: Do you think your code should handle empty lists or multiple nodes with the same value?
Concept: Implement the delete-by-value function in TypeScript, covering empty lists and deleting only the first matching node.
```typescript class ListNode { value: number; next: ListNode | null; constructor(value: number) { this.value = value; this.next = null; } } function deleteNodeByValue(head: ListNode | null, value: number): ListNode | null { if (!head) return null; // If head needs to be deleted if (head.value === value) { return head.next; } let current = head; while (current.next !== null) { if (current.next.value === value) { current.next = current.next.next; break; // delete only first occurrence } current = current.next; } return head; } // Example usage: // List: 1 -> 2 -> 3 -> 4 -> null // Delete value 3 const head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); head.next.next.next = new ListNode(4); const newHead = deleteNodeByValue(head, 3); // Resulting list: 1 -> 2 -> 4 -> null ```
Result
The node with value 3 is removed, resulting in the list: 1 -> 2 -> 4 -> null
Implementing with real code solidifies understanding and shows how to handle edge cases.
7
ExpertPerformance and Multiple Deletions
🤔Before reading on: Do you think deleting multiple nodes with the same value requires a different approach than deleting just one?
Concept: Deleting all nodes with a given value requires looping through the entire list and careful pointer updates.
To delete all nodes with a target value, you must check every node, including the head repeatedly if needed. This involves a loop that continues until no matching nodes remain. Careful handling of the head pointer and previous nodes is required to avoid skipping nodes or breaking the list.
Result
All nodes with the target value are removed, and the list remains connected.
Knowing how to extend deletion to multiple nodes prepares you for real-world scenarios where duplicates exist.
Under the Hood
Each node in a linked list holds a value and a pointer to the next node. Deleting a node by value means changing the pointer of the previous node to point to the node after the one being deleted. This removes the node from the chain without copying or moving data. The garbage collector or memory manager then frees the removed node's memory if no references remain.
Why designed this way?
Linked lists were designed to allow efficient insertions and deletions without shifting elements, unlike arrays. Changing pointers is faster and uses less memory than moving data. This design trades off direct access speed for flexible dynamic updates.
Head -> [Node1|Val] -> [Node2|Val] -> [Node3|Val] -> null

Delete Node2:
Head -> [Node1|Val] --------> [Node3|Val] -> null

Pointer change:
Node1.next = Node3
Myth Busters - 4 Common Misconceptions
Quick: When deleting a node by value, do you think you must delete all nodes with that value or just the first one? Commit to your answer.
Common Belief:Deleting a node by value removes all nodes with that value automatically.
Tap to reveal reality
Reality:Typically, only the first node with the matching value is deleted unless explicitly coded to remove all.
Why it matters:Assuming all nodes are deleted can cause bugs where duplicates remain unexpectedly.
Quick: Do you think deleting the head node requires the same pointer changes as deleting a middle node? Commit to yes or no.
Common Belief:Deleting any node is the same process regardless of position.
Tap to reveal reality
Reality:Deleting the head node requires updating the head pointer itself, unlike middle nodes where previous.next is updated.
Why it matters:Not handling the head node separately can cause loss of the entire list.
Quick: Do you think you can delete a node by value without traversing the list? Commit to yes or no.
Common Belief:You can delete a node by value instantly without searching.
Tap to reveal reality
Reality:You must traverse the list to find the node with the target value before deleting it.
Why it matters:Skipping traversal leads to incorrect deletions or runtime errors.
Quick: Do you think deleting a node frees its memory immediately in all languages? Commit to your answer.
Common Belief:Deleting a node immediately frees its memory.
Tap to reveal reality
Reality:In languages with garbage collection like TypeScript, memory is freed later when no references remain.
Why it matters:Misunderstanding memory management can cause memory leaks or performance issues.
Expert Zone
1
Deleting nodes in a singly linked list requires tracking the previous node, but doubly linked lists simplify this by having backward pointers.
2
When deleting multiple nodes with the same value, careful pointer updates are needed to avoid skipping nodes after deletion.
3
In concurrent environments, deleting nodes safely requires synchronization to avoid race conditions or corrupted lists.
When NOT to use
Deleting nodes by value is inefficient for very large lists with many duplicates; in such cases, using a hash-based structure or balanced tree may be better for faster lookups and deletions.
Production Patterns
In real systems, delete-by-value is often combined with search functions and may include flags for lazy deletion or batch removals to optimize performance and reduce fragmentation.
Connections
Hash Tables
Builds-on
Understanding linked list deletion helps grasp how hash tables handle collisions using chaining, where linked lists store multiple values per bucket.
Memory Management
Underlying principle
Deleting nodes connects directly to how memory is freed or reused, linking data structure operations to system-level resource management.
Supply Chain Management
Analogous process
Removing a node by value is like removing a faulty item from a supply chain without breaking the flow, showing how concepts in logistics mirror data structure operations.
Common Pitfalls
#1Forgetting to update the head pointer when deleting the first node.
Wrong approach:function deleteNode(head, value) { let current = head; while (current !== null) { if (current.value === value) { current = current.next; // wrong: does not update head break; } current = current.next; } return head; }
Correct approach:function deleteNode(head, value) { if (head && head.value === value) { return head.next; } let current = head; while (current && current.next) { if (current.next.value === value) { current.next = current.next.next; break; } current = current.next; } return head; }
Root cause:Misunderstanding that the head pointer must be reassigned when deleting the first node.
#2Not tracking the previous node when deleting a middle node.
Wrong approach:function deleteNode(head, value) { let current = head; while (current !== null) { if (current.value === value) { current = current.next; // wrong: does not update previous.next break; } current = current.next; } return head; }
Correct approach:function deleteNode(head, value) { if (!head) return null; if (head.value === value) return head.next; let current = head; while (current.next !== null) { if (current.next.value === value) { current.next = current.next.next; break; } current = current.next; } return head; }
Root cause:Confusing the current node with the previous node and not updating the link that points to the node to delete.
#3Trying to delete a node without checking if the list is empty.
Wrong approach:function deleteNode(head, value) { let current = head; while (current.next !== null) { if (current.next.value === value) { current.next = current.next.next; break; } current = current.next; } return head; }
Correct approach:function deleteNode(head, value) { if (!head) return null; if (head.value === value) return head.next; let current = head; while (current.next !== null) { if (current.next.value === value) { current.next = current.next.next; break; } current = current.next; } return head; }
Root cause:Not handling the empty list case leads to runtime errors when accessing properties of null.
Key Takeaways
Deleting a node by value in a linked list means changing pointers to skip the node with that value, keeping the list connected.
Special care is needed when deleting the head node because the head pointer itself must be updated.
Traversal is necessary to find the node to delete, and only the first matching node is removed unless otherwise specified.
Proper pointer updates prevent breaking the list or losing access to nodes.
Understanding deletion by value prepares you for more complex linked list operations and real-world data management.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a linked list?
easy
A. Reverse the linked list
B. Add a new node with the given value at the end
C. Remove the first node that contains the given value
D. Find the position of a node with the given value

Solution

  1. Step 1: Understand the operation goal

    The operation aims to remove a node that matches a specific value from the linked list.
  2. Step 2: Identify the correct action

    Removing the first node with the given value means deleting that node and linking its previous node to the next node.
  3. Final Answer:

    Remove the first node that contains the given value -> Option C
  4. Quick Check:

    Delete node by value = Remove first matching node [OK]
Hint: Delete node by value means remove first matching node [OK]
Common Mistakes:
  • Confusing deletion with insertion
  • Trying to delete all nodes with the value instead of first
  • Thinking it finds position only
  • Assuming it reverses the list
2. Which of the following TypeScript code snippets correctly deletes the head node if its value is 5 from a singly linked list starting at head?
easy
A. if (head.value === 5) head = head.next;
B. head = head.next.value === 5 ? head.next.next : head;
C. while (head.value !== 5) head = head.next;
D. head.next = null;

Solution

  1. Step 1: Check if head node has value 5

    If the first node's value is 5, we remove it by moving head to the next node.
  2. Step 2: Verify code correctness

    if (head.value === 5) head = head.next; correctly updates head to head.next if head.value is 5, effectively deleting the first node.
  3. Final Answer:

    if (head.value === 5) head = head.next; -> Option A
  4. Quick Check:

    Delete head node by reassigning head [OK]
Hint: Check head value first, then update head pointer [OK]
Common Mistakes:
  • Using incorrect conditional expressions
  • Assigning head.next incorrectly
  • Not handling head node deletion
  • Overwriting head without checks
3. Given the linked list: 1 -> 3 -> 5 -> 7 -> null, what is the list after deleting the node with value 5 using the following TypeScript code?
let current = head;
while (current.next !== null) {
  if (current.next.value === 5) {
    current.next = current.next.next;
    break;
  }
  current = current.next;
}
medium
A. 1 -> 3 -> 5 -> 7 -> null
B. 1 -> 3 -> 7 -> null
C. 3 -> 5 -> 7 -> null
D. 1 -> 7 -> null

Solution

  1. Step 1: Traverse list to find node before value 5

    Loop checks nodes until current.next.value is 5, which is true when current is node with value 3.
  2. Step 2: Delete node with value 5 by skipping it

    Set current.next to current.next.next, linking node 3 directly to node 7, removing node 5.
  3. Final Answer:

    1 -> 3 -> 7 -> null -> Option B
  4. Quick Check:

    Skip node with value 5 = 1->3->7 [OK]
Hint: Skip node by linking previous to next [OK]
Common Mistakes:
  • Not breaking loop after deletion
  • Deleting wrong node
  • Not updating pointers correctly
  • Confusing current and current.next
4. Consider this buggy TypeScript code to delete a node by value from a singly linked list:
function deleteNode(head: Node | null, value: number): Node | null {
  let current = head;
  while (current !== null) {
    if (current.next?.value === value) {
      current.next = current.next.next;
    }
    current = current.next!;
  }
  return head;
}
What is the main problem with this code?
medium
A. It may cause a runtime error when current.next is null
B. It deletes all nodes with the value instead of the first
C. It does not handle deletion if head node has the value
D. It reverses the linked list accidentally

Solution

  1. Step 1: Analyze the loop termination

    The while (current !== null) continues to the last node where current.next is null. The if uses ?. safely, but then executes current = current.next!;, and ! non-null assertion on null causes runtime error.
  2. Step 2: Confirm the bug triggers always

    For any non-empty list, it crashes at the last node regardless of value, preventing correct execution.
  3. Final Answer:

    It may cause a runtime error when current.next is null -> Option A
  4. Quick Check:

    current.next! on null = runtime error [OK]
Hint: Beware of non-null assertion ! on potentially null next [OK]
Common Mistakes:
  • Using non-null assertion (!) on potentially null next
  • Assuming current.next always exists
  • Not breaking after deletion
  • Confusing current and current.next updates
5. You have a singly linked list: 2 -> 4 -> 6 -> 4 -> 8 -> null. You want to delete the first node with value 4. After deletion, you want to insert a new node with value 10 right after the deleted node's position. Which sequence of operations correctly achieves this?
hard
A. Delete first 4, then insert 10 after node 6
B. Insert 10 at head, then delete first 4
C. Delete all 4s, then insert 10 at end
D. Delete first 4, then insert 10 after node 2

Solution

  1. Step 1: Delete first node with value 4

    The first 4 is the second node, so remove it by linking node 2 to node 6.
  2. Step 2: Insert new node with value 10 after node 2

    Create new node 10 and link it after node 2, then link new node to node 6.
  3. Final Answer:

    Delete first 4, then insert 10 after node 2 -> Option D
  4. Quick Check:

    Delete 4, insert 10 after previous node [OK]
Hint: Delete node, then insert new node at same position [OK]
Common Mistakes:
  • Inserting new node at wrong position
  • Deleting all nodes with value 4
  • Not updating links correctly
  • Inserting before deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard