Bird
Raised Fist0
DSA Goprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete by Value in Doubly Linked List
Start at head
↓
Check if current node's value == target?
No→Move to next node
↓
If current == null
↓
Adjust pointers of prev and next nodes
↓
Delete current node
↓
Stop or continue if multiple deletions
Start from the head node, check each node's value. If it matches the target, adjust the previous and next pointers to remove the node, then delete it. Stop when the node is deleted or list ends.
Execution Sample
DSA Go
func DeleteByValue(head *Node, val int) *Node {
  current := head
  for current != nil {
    if current.value == val {
      // adjust pointers and delete
      break
    }
    current = current.next
  }
  return head
}
This code searches for a node with the given value and deletes it by adjusting pointers in a doubly linked list.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1None1 <-> 2 <-> 3 <-> 4 <-> 5
2Check current node value == 3?1None1 <-> 2 <-> 3 <-> 4 <-> 5
3Move to next node2None1 <-> 2 <-> 3 <-> 4 <-> 5
4Check current node value == 3?2None1 <-> 2 <-> 3 <-> 4 <-> 5
5Move to next node3None1 <-> 2 <-> 3 <-> 4 <-> 5
6Check current node value == 3?3None1 <-> 2 <-> 3 <-> 4 <-> 5
7Adjust pointers to remove node 332.next = 4, 4.prev = 21 <-> 2 <-> 4 <-> 5
8Delete node 33Node 3 removed1 <-> 2 <-> 4 <-> 5
9Stop - node deletedN/ANone1 <-> 2 <-> 4 <-> 5
💡 Node with value 3 found and deleted; list updated accordingly.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
currentNode(1)Node(2)Node(3)Node(3)nil
headNode(1)Node(1)Node(1)Node(1)Node(1)
list size5 nodes5 nodes5 nodes4 nodes4 nodes
Key Moments - 3 Insights
Why do we adjust both prev and next pointers when deleting a node?
Because the node is connected in both directions, 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 7 of the execution_table.
What happens if the node to delete is the head?
If the head node matches the value, we update the head pointer to the next node and adjust its prev pointer to nil. This is a special case not shown here but follows the same pointer adjustment logic.
Why do we stop after deleting the first matching node?
The code is designed to delete only the first occurrence of the value. After adjusting pointers and deleting the node (step 8), the loop breaks and stops further traversal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 5?
ANode with value 3
BNode with value 2
CNode with value 4
Dnil
💡 Hint
Check the 'Current Node Value' column at step 5 in the execution_table.
At which step do the pointers get adjusted to remove the node?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look for 'Adjust pointers' in the 'Operation' column in the execution_table.
If we wanted to delete all nodes with value 3, how would the execution_table change?
AIt would stop after first deletion as now
BIt would continue traversal after step 8 to find more nodes
CIt would delete nodes without adjusting pointers
DIt would delete nodes before checking their value
💡 Hint
Refer to the 'Stop' operation at step 9 and consider what happens if we don't break the loop.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head node
- Traverse nodes checking value
- If match, adjust prev.next and next.prev pointers
- Remove node from list
- Stop after first deletion or continue for all
- Handle head/tail edge cases carefully
Full Transcript
To delete a node by value in a doubly linked list, start at the head and check each node's value. When the target value is found, adjust the previous node's next pointer and the next node's previous pointer to bypass the current node. Then delete the current node. The traversal stops after deleting the first matching node. Special cases include deleting the head or tail nodes, which require updating the head pointer or tail pointer accordingly. This process ensures the list remains connected and valid after deletion.

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 doubly linked list from head to tail to find the node whose data matches the value to delete.
  2. Step 2: Update the links to remove the node

    Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted, effectively removing it from the list.
  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 and fix links [OK]
Hint: Find node by value, then fix previous and next links [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node when multiple have same value
2. Which of the following Go code snippets correctly deletes a node by value from a doubly linked list?
easy
A. if current.data == value { current.next.next = current.prev.prev }
B. if current.data == value { current.next = current.prev; current.prev = current.next }
C. if current.data == value { current = nil }
D. if current.data == value { current.prev.next = current.next; current.next.prev = current.prev }

Solution

  1. Step 1: Check node data against value

    The condition current.data == value correctly identifies the node to delete.
  2. Step 2: Update links correctly

    Setting current.prev.next = current.next and current.next.prev = current.prev properly removes the node by linking its neighbors.
  3. Final Answer:

    if current.data == value { current.prev.next = current.next; current.next.prev = current.prev } -> Option D
  4. Quick Check:

    Correct link updates remove node [OK]
Hint: Update prev.next and next.prev to remove node [OK]
Common Mistakes:
  • Swapping prev and next pointers incorrectly
  • Setting current to nil without fixing links
  • Accessing next or prev without nil checks causing panic
3. Given the doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5, what is the list after deleting the node with value 3?
medium
A. 1 <-> 2 <-> 4 <-> 3 <-> 5
B. 1 <-> 2 <-> 4 <-> 5
C. 2 <-> 3 <-> 4 <-> 5
D. 1 <-> 2 <-> 3 <-> 5

Solution

  1. Step 1: Identify node with value 3

    Traverse the list to find the node containing 3, which is between nodes 2 and 4.
  2. Step 2: Remove node 3 and update links

    Set node 2's next pointer to node 4 and node 4's previous pointer to node 2, skipping node 3.
  3. Final Answer:

    1 <-> 2 <-> 4 <-> 5 -> Option B
  4. Quick Check:

    Node 3 removed, neighbors linked [OK]
Hint: Remove node and link neighbors directly [OK]
Common Mistakes:
  • Leaving node 3 linked causing broken list
  • Deleting wrong node by value
  • Not updating both prev and next pointers
4. Consider this Go code snippet to delete a node by value in a doubly linked list:
func deleteByValue(head *Node, value int) *Node {
    current := head
    for current != nil {
        if current.data == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            }
            break
        }
        current = current.next
    }
    return head
}
What is the bug in this code?
medium
A. It does not handle deleting the last node correctly
B. It does not update head when deleting the first node
C. It breaks after deleting only the first matching node
D. It does not check for nil pointers before accessing prev or next

Solution

  1. Step 1: Analyze loop and break behavior

    The loop breaks immediately after deleting the first node with matching value, so only one node is deleted even if multiple nodes have the same value.
  2. Step 2: Understand implications for multiple matches

    If the list has multiple nodes with the same value, only the first is removed, which may not be the intended behavior if all matches should be deleted.
  3. Final Answer:

    It breaks after deleting only the first matching node -> Option C
  4. Quick Check:

    Break stops after first delete [OK]
Hint: Check if loop breaks too early when deleting multiple nodes [OK]
Common Mistakes:
  • Ignoring multiple nodes with same value
  • Not updating head when first node deleted
  • Not checking nil before pointer access
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 20 <-> 40. You want to delete all nodes with value 20. Which approach correctly deletes all such nodes?
hard
A. Traverse the list and delete nodes with value 20, continuing without breaking the loop
B. Delete the first node with value 20 and then stop traversal
C. Delete nodes with value 20 only if they are at the head or tail
D. Replace nodes with value 20 with a new node containing 0

Solution

  1. Step 1: Traverse entire list without breaking

    To delete all nodes with value 20, you must check every node and not stop after the first deletion.
  2. Step 2: Delete matching nodes and update links carefully

    For each node with value 20, update the previous and next pointers to remove it, then continue traversal to find others.
  3. Final Answer:

    Traverse the list and delete nodes with value 20, continuing without breaking the loop -> Option A
  4. Quick Check:

    Delete all matches by full traversal [OK]
Hint: Do not break loop; delete all matching nodes during traversal [OK]
Common Mistakes:
  • Breaking loop after first deletion
  • Not updating links properly causing broken list
  • Only deleting head or tail 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