Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Delete by Value in Doubly Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete by Value in Doubly Linked List
Start at head
↓
Check if head is null
↓
Traverse nodes
↓
Is current.data == value?
No→Move to next node
Yes↓
Adjust pointers of prev and next nodes
↓
Delete current node
↓
Stop or continue if multiple deletions
↓
Done
Start from the head, move through nodes until the value is found, then adjust pointers to remove the node and continue or stop.
Execution Sample
DSA Typescript
class Node {
  data: number;
  prev: Node | null;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

function deleteByValue(head: Node | null, value: number): Node | null {
  if (!head) return null;
  let current = head;

  while (current) {
    if (current.data === value) {
      if (current.prev) {
        current.prev.next = current.next;
      } else {
        head = current.next;
      }
      if (current.next) {
        current.next.prev = current.prev;
      }
      break;
    }
    current = current.next;
  }
  return head;
}
Deletes the first node with the given value from a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start at head1 <-> 2 <-> 3 <-> 4 <-> 5head points to node with data 1head -> [1] <-> [2] <-> [3] <-> [4] <-> [5]
2Check current node data == 3?1 <-> 2 <-> 3 <-> 4 <-> 5No pointer changehead -> [1] <-> [2] <-> [3] <-> [4] <-> [5]
3Move to next node1 <-> 2 <-> 3 <-> 4 <-> 5current moves from 2 to 3head -> [1] <-> [2] <-> [3] <-> [4] <-> [5]
4Check current node data == 3?1 <-> 2 <-> 3 <-> 4 <-> 5No pointer changehead -> [1] <-> [2] <-> [3] <-> [4] <-> [5]
5Delete node with data 31 <-> 2 <-> 4 <-> 52.next = 4, 4.prev = 2head -> [1] <-> [2] <-> [4] <-> [5]
6Stop after deletion1 <-> 2 <-> 4 <-> 5No pointer changehead -> [1] <-> [2] <-> [4] <-> [5]
💡 Node with value 3 found and deleted, operation stops.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
headNode(1)Node(1)Node(1)Node(1)
currentNode(1)Node(3)Node(3)Node(3)
prevnullNode(2)Node(2)Node(2)
list size5544
Key Moments - 3 Insights
Why do we update both prev.next and next.prev pointers when deleting a node?
Because in a doubly linked list, each node points to both previous and next nodes. To remove a node cleanly, we must link its previous node's next pointer to its next node, and its next node's prev pointer back to its previous node. This is shown in step 5 where 2.next is set to 4 and 4.prev is set to 2.
What happens if the node to delete is the head node?
If the head node matches the value, we update the head pointer to the next node and set the new head's prev pointer to null. This is not shown in this trace but follows the same pointer adjustment logic.
Why do we stop after deleting the first matching node?
Because the function is designed to delete only the first occurrence of the value. After deletion at step 5, the operation stops at step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the visual state after step 5?
Ahead -> [1] <-> [2] <-> [3] <-> [4] <-> [5]
Bhead -> [1] <-> [2] <-> [4] <-> [5]
Chead -> [1] <-> [3] <-> [4] <-> [5]
Dhead -> [2] <-> [4] <-> [5]
💡 Hint
Check the 'Visual State' column for step 5 in the execution table.
At which step does the pointer 'current' move from node 2 to node 3?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Pointer Changes' column to see when current moves.
If the value to delete was 1 (head node), what pointer change would you expect?
Ahead moves to node 2, node 2.prev set to null
Bhead moves to node 3, node 3.prev set to null
Chead remains node 1, no pointer changes
Dhead moves to node 5, node 5.prev set to null
💡 Hint
Recall that deleting head requires updating head pointer and new head's prev pointer.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head node
- Traverse nodes until value found
- Adjust prev.next and next.prev pointers to remove node
- Update head if deleting first node
- Stop after first deletion
- List remains connected without deleted node
Full Transcript
This visualization shows how to delete a node by value in a doubly linked list. We start at the head and move through nodes checking each node's data. When we find the node with the target value, we adjust the pointers of the previous and next nodes to bypass the node to delete. If the node is the head, we update the head pointer. After deletion, the list remains connected without the deleted node. The operation stops after deleting the first matching node.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. All nodes are deleted regardless of value
B. The node with the matching value is removed and links are updated
C. Only the first node is deleted no matter the value
D. The list is reversed instead of deleting

Solution

  1. Step 1: Identify the node with the given value

    Traverse the list from head to tail to find the node whose value matches the target.
  2. Step 2: Remove the node and update links

    Adjust the previous node's next pointer and the next node's previous pointer to exclude the found node.
  3. Final Answer:

    The node with the matching value is removed and links are updated -> Option B
  4. Quick Check:

    Deleting by value removes matching node [OK]
Hint: Find node by value, then update previous and next links [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting all nodes instead of one
2. Which of the following is the correct TypeScript syntax to delete a node by value in a doubly linked list?
easy
A. function deleteNode(value) { /* code */ }
B. removeNode(value: number) => { /* code */ }
C. deleteNode(value: number): void { /* code */ }
D. delete node(value: number) { /* code */ }

Solution

  1. Step 1: Check method declaration syntax in TypeScript class

    Methods inside classes use the syntax methodName(params): returnType { body }.
  2. Step 2: Identify correct syntax for deleteNode method

    deleteNode(value: number): void { /* code */ } uses correct TypeScript method syntax with type annotation and void return.
  3. Final Answer:

    deleteNode(value: number): void { /* code */ } -> Option C
  4. Quick Check:

    TypeScript method syntax = deleteNode(value: number): void { /* code */ } [OK]
Hint: Use methodName(params): returnType { } inside class [OK]
Common Mistakes:
  • Using arrow function syntax inside class method
  • Missing type annotations
  • Using invalid keywords like 'delete node'
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> 40, what is the list after deleting the node with value 30?
medium
A. 10 <-> 20 <-> 40
B. 10 <-> 30 <-> 40
C. 20 <-> 30 <-> 40
D. 10 <-> 20 <-> 30 <-> 40

Solution

  1. Step 1: Locate node with value 30

    Traverse the list: 10 -> 20 -> 30 found at third node.
  2. Step 2: Remove node 30 and link 20 to 40

    Update 20's next pointer to 40 and 40's previous pointer to 20.
  3. Final Answer:

    10 <-> 20 <-> 40 -> Option A
  4. Quick Check:

    Deleted 30, list links updated [OK]
Hint: Remove node, link previous and next nodes directly [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node
4. What is wrong with this delete function snippet for doubly linked list?
deleteNode(value: number): void {
  let current = this.head;
  while (current !== null) {
    if (current.value === value) {
      current.prev.next = current.next;
      current.next.prev = current.prev;
      break;
    }
    current = current.next;
  }
}
medium
A. It does not handle deleting the head node correctly
B. It deletes all nodes with the value instead of one
C. It uses wrong variable names
D. It causes infinite loop

Solution

  1. Step 1: Check handling of head node deletion

    If current is head, current.prev is null, so current.prev.next causes error.
  2. Step 2: Identify missing head update

    Function does not update this.head when deleting the first node, causing broken list.
  3. Final Answer:

    It does not handle deleting the head node correctly -> Option A
  4. Quick Check:

    Head deletion needs special handling [OK]
Hint: Check if node is head before updating prev pointers [OK]
Common Mistakes:
  • Not checking if current.prev is null
  • Not updating head pointer when deleting first node
  • Assuming all nodes have prev and next
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> 25. You want to delete the node with value 5, then delete the node with value 25. What is the final list?
hard
A. 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20
C. 10 <-> 15 <-> 20 <-> 25
D. 10 <-> 15 <-> 20

Solution

  1. Step 1: Delete node with value 5 (head node)

    Update head to 10, set 10.prev to null.
  2. Step 2: Delete node with value 25 (tail node)

    Update 20.next to null, removing 25.
  3. Final Answer:

    10 <-> 15 <-> 20 -> Option D
  4. Quick Check:

    Deleted head and tail nodes correctly [OK]
Hint: Update head or tail pointers when deleting first or last nodes [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating tail when deleting last node
  • Breaking links between middle nodes
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