Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete by Value in Doubly Linked List
Start at head
↓
Check if current node value == target?
No→Move to next node
↓
Reached end?
Yes→Value not found, stop
↓
Delete current node by adjusting pointers
↓
Done
Start from the head node, check each node's value. If it matches the target, adjust pointers to remove it. Otherwise, move to next node until end or found.
Execution Sample
DSA Javascript
function deleteByValue(head, value) {
  let current = head;
  while (current) {
    if (current.val === value) {
      if (!current.prev) {
        head = current.next;
        if (head) {
          head.prev = null;
        }
      } else {
        current.prev.next = current.next;
      }
      if (current.next) {
        current.next.prev = current.prev;
      }
      break;
    }
    current = current.next;
  }
  return head;
}
This code searches the doubly linked list for a node with the given value and deletes it by adjusting pointers.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1NoneHead -> 1 <-> 2 <-> 3 <-> 4 <-> null
2Check if current.val == 31NoneHead -> 1 <-> 2 <-> 3 <-> 4 <-> null
3Move to next node2NoneHead -> 1 <-> 2 <-> 3 <-> 4 <-> null
4Check if current.val == 32NoneHead -> 1 <-> 2 <-> 3 <-> 4 <-> null
5Move to next node3NoneHead -> 1 <-> 2 <-> 3 <-> 4 <-> null
6Check if current.val == 33prev.next = current.next; next.prev = current.prevHead -> 1 <-> 2 <-> 4 <-> null (3 removed)
7Stop after deletion3NoneHead -> 1 <-> 2 <-> 4 <-> null
💡 Node with value 3 found and deleted at step 6, traversal stops.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
current.val12333
head11111
list size44433
Key Moments - 3 Insights
Why do we adjust both prev.next and next.prev when deleting a node?
Because the list is doubly linked, we must update the previous node's next pointer and the next node's prev pointer to bypass the deleted node, as shown in step 6 of the execution_table.
What happens if the node to delete is the head?
If the head node is deleted, the head pointer must be updated to the next node. This example deletes a middle node, but the same pointer adjustments apply, with head updated if needed.
Why do we stop traversal immediately after deletion?
Because the problem deletes only the first occurrence of the value. After adjusting pointers at step 6, we stop to avoid further traversal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current.val at step 5?
A3
B1
C2
D4
💡 Hint
Check the 'Current Node Value' column at step 5 in the execution_table.
At which step does the node with value 3 get removed from the list?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the step where pointer changes happen and the visual state shows 3 removed.
If the value to delete was 1 (head), how would the pointer changes differ?
ABoth prev.next and next.prev change, head unchanged
BOnly prev.next would change, head stays same
COnly next.prev would change, head updated to next node
DNo pointer changes needed
💡 Hint
Deleting head requires updating head pointer and next node's prev pointer.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head, traverse nodes
- If node.val == target, adjust pointers:
  prev.next = current.next
  next.prev = current.prev
- Update head if deleting first node
- Stop after first deletion
- List remains connected without deleted node
Full Transcript
To delete a node by value in a doubly linked list, start at the head and check each node's value. If the value matches, adjust the previous node's next pointer and the next node's previous pointer to bypass the current node. If the node is the head, update the head pointer to the next node. Stop traversal after deleting the first matching node. This keeps the list connected and removes the target node safely.

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