Bird
Raised Fist0
DSA Javascriptprogramming

Delete by Value in Doubly Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
We find the node with the value and remove it by linking its previous and next nodes directly.
Analogy: Imagine a train with carriages linked front and back. Removing one carriage means connecting the one before it directly to the one after it.
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
          ↑
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3 ↔ 4, delete value 3
Goal: Remove the node with value 3 and keep the list connected
Step 1: start at head node with value 1
null ← [curr->1] ↔ 2 ↔ 3 ↔ 4 -> null
Why: We begin searching from the start to find the node to delete
Step 2: move curr to next node with value 2
null ← 1 ↔ [curr->2] ↔ 3 ↔ 4 -> null
Why: Value 1 is not 3, so continue searching
Step 3: move curr to next node with value 3
null ← 1 ↔ 2 ↔ [curr->3] ↔ 4 -> null
Why: Found the node with value 3 to delete
Step 4: link node 2's next to node 4 and node 4's prev to node 2, removing node 3
null ← 1 ↔ 2 ↔ 4 -> null
Why: By skipping node 3, we remove it and keep the list connected
Result:
null ← 1 ↔ 2 ↔ 4 -> null
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteByValue(value) {
    let curr = this.head;
    while (curr) {
      if (curr.value === value) {
        if (curr.prev) {
          curr.prev.next = curr.next; // link prev node to next node
        } else {
          this.head = curr.next; // deleting head
        }
        if (curr.next) {
          curr.next.prev = curr.prev; // link next node to prev node
        } else {
          this.tail = curr.prev; // deleting tail
        }
        return; // delete only first occurrence
      }
      curr = curr.next; // move to next node
    }
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr) {
      result += curr.value;
      if (curr.next) result += ' ↔ ';
      curr = curr.next;
    }
    console.log(result);
  }
}

// Driver code
const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.deleteByValue(3);
dll.printList();
while (curr) {
traverse nodes to find target value
if (curr.value === value) {
check if current node has the value to delete
if (curr.prev) { curr.prev.next = curr.next; } else { this.head = curr.next; }
link previous node's next to current's next or update head if deleting first node
if (curr.next) { curr.next.prev = curr.prev; } else { this.tail = curr.prev; }
link next node's prev to current's prev or update tail if deleting last node
return;
stop after deleting first matching node
curr = curr.next;
move to next node to continue search
OutputSuccess
1 ↔ 2 ↔ 4
Complexity Analysis
Time: O(n) because we may need to check each node once to find the value
Space: O(1) because we only use a few pointers, no extra storage
vs Alternative: Compared to singly linked list deletion, doubly linked list allows direct access to previous node, making deletion easier without extra traversal
Edge Cases
empty list
nothing happens, no error
DSA Javascript
while (curr) {
delete head node
head pointer updates to next node
DSA Javascript
else { this.head = curr.next; }
delete tail node
tail pointer updates to previous node
DSA Javascript
else { this.tail = curr.prev; }
value not found
list remains unchanged
DSA Javascript
while (curr) {
When to Use This Pattern
When you need to remove a node by value in a list that has links both ways, use the delete by value in doubly linked list pattern to efficiently update neighbors.
Common Mistakes
Mistake: Not updating the head or tail pointers when deleting the first or last node
Fix: Add checks to update head if deleting first node and tail if deleting last node
Mistake: Not linking previous node's next to current's next, causing broken list
Fix: Always link curr.prev.next to curr.next if prev exists
Mistake: Not linking next node's prev to current's prev, causing broken backward links
Fix: Always link curr.next.prev to curr.prev if next exists
Summary
Deletes the first node with a given value by reconnecting its neighbors in a doubly linked list.
Use when you want to remove a specific value from a doubly linked list efficiently.
The key is to update both previous and next nodes to skip the deleted node, and adjust head or tail if needed.

Practice

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

Solution

  1. Step 1: Locate 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 bypass the deleted node.
  3. Final Answer:

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

    Delete by value means remove matching node [OK]
Hint: Delete node by value means remove matching node and fix links [OK]
Common Mistakes:
  • Deleting all nodes instead of one
  • Not updating previous or next pointers
  • Deleting wrong node when multiple have same value
2. Which of the following is the correct way to check if the current node's value matches the target value in JavaScript?
easy
A. if (current.value != target) {}
B. if (current.value == target) {}
C. if (current.value = target) {}
D. if (current.value === target) {}

Solution

  1. Step 1: Understand JavaScript equality operators

    Use === for strict equality to avoid type coercion and ensure exact match.
  2. Step 2: Identify correct syntax for comparison

    if (current.value === target) correctly compares values and types.
  3. Final Answer:

    if (current.value === target) {} -> Option D
  4. Quick Check:

    Use strict equality (===) for value comparison [OK]
Hint: Use === for exact value and type match in JavaScript [OK]
Common Mistakes:
  • Using single = which assigns instead of compares
  • Using == which allows type coercion
  • Using != which checks inequality
3. Consider this doubly linked list: 10 <-> 20 <-> 30 <-> 40. After deleting the node with value 30, what is the new list?
medium
A. 10 <-> 20 <-> 30 <-> 40
B. 10 <-> 20 <-> 40
C. 20 <-> 30 <-> 40
D. 10 <-> 30 <-> 40

Solution

  1. Step 1: Identify node to delete

    The node with value 30 is located between 20 and 40.
  2. Step 2: Remove node 30 and update links

    Set 20's next pointer to 40 and 40's previous pointer to 20, skipping 30.
  3. Final Answer:

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

    Deleted 30, linked 20 and 40 directly [OK]
Hint: Remove node and link neighbors directly [OK]
Common Mistakes:
  • Not updating previous pointer of next node
  • Leaving deleted node linked in list
  • Deleting wrong node
4. Given this code snippet to delete a node by value, what is the error?
function deleteByValue(head, value) {
  let current = head;
  while (current !== null) {
    if (current.value === value) {
      current.prev.next = current.next;
      current.next.prev = current.prev;
      break;
    }
    current = current.next;
  }
  return head;
}
medium
A. It uses assignment instead of comparison
B. It never breaks the loop
C. It does not handle deleting the head node correctly
D. It returns null always

Solution

  1. Step 1: Analyze deletion of head node

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

    Code should update head to current.next if deleting head node to avoid broken list.
  3. Final Answer:

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

    Deleting head 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 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 (head) and then the node with value 25 (tail). What is the final list?
hard
A. 10 <-> 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20
C. 10 <-> 15 <-> 20 <-> 25
D. 15 <-> 20

Solution

  1. Step 1: Delete head node with value 5

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

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

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

    Removed head and tail nodes correctly [OK]
Hint: Update head or tail pointers when deleting ends [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating tail when deleting last node
  • Leaving dangling pointers
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