Bird
Raised Fist0
DSA Cprogramming~10 mins

Delete by Value in Doubly Linked List in DSA C - 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
↓
Value not found, stop
↓
Delete current node
↓
Stop
Start from the head, look for the node with the target value. If found, adjust the previous and next nodes' pointers to remove it, then delete the node.
Execution Sample
DSA C
void deleteByValue(Node** head, int val) {
  Node* current = *head;
  while (current != NULL && current->data != val) {
    current = current->next;
  }
  if (current == NULL) return;
  if (current->prev) current->prev->next = current->next;
  else *head = current->next;
  if (current->next) current->next->prev = current->prev;
  free(current);
}
This code searches for a node with the given value and deletes it from the doubly linked list by adjusting pointers.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1None1 <-> 2 <-> 3 <-> 4 <-> NULL
2Check if current->data == 31None1 <-> 2 <-> 3 <-> 4 <-> NULL
3Move to next node2None1 <-> 2 <-> 3 <-> 4 <-> NULL
4Check if current->data == 32None1 <-> 2 <-> 3 <-> 4 <-> NULL
5Move to next node3None1 <-> 2 <-> 3 <-> 4 <-> NULL
6Check if current->data == 33None1 <-> 2 <-> 3 <-> 4 <-> NULL
7Adjust pointers to remove node 332->next = 4, 4->prev = 21 <-> 2 <-> 4 <-> NULL
8Delete node 33Node 3 freed1 <-> 2 <-> 4 <-> NULL
9StopNULLNone1 <-> 2 <-> 4 <-> NULL
💡 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)NULL
headNode(1)Node(1)Node(1)Node(1)Node(1)
list size4 nodes4 nodes4 nodes3 nodes3 nodes
Key Moments - 3 Insights
Why do we check if current->prev is NULL before changing pointers?
Because if current->prev is NULL, it means current is the head node. We must update the head pointer instead of current->prev->next. This is shown in step 7 where pointer changes depend on whether current->prev exists.
What happens if the value to delete is not found in the list?
The loop ends with current == NULL (step 9), and the function returns without changes. The list remains the same, as no pointer adjustments or deletions occur.
Why do we update both prev and next 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 to its next node and vice versa, as shown in step 7.
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
DNULL
💡 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' operation in the execution_table.
If the node to delete was the head (value 1), what pointer would be updated?
Acurrent->prev->next
Bhead pointer
Ccurrent->next->prev
DNo pointer changes
💡 Hint
Refer to key_moments about updating head when current->prev is NULL.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head, traverse nodes until value found
- If node found:
  - If prev exists, prev->next = current->next
  - Else update head to current->next
  - If next exists, next->prev = current->prev
- Free the node
- If not found, do nothing
Full Transcript
To delete a node by value in a doubly linked list, start from the head and move through nodes until you find the node with the target value. If the node is found, 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. Finally, free the memory of the deleted node. If the value is not found, the list remains unchanged.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. Only the first node is deleted without checking value
B. The node with the matching value is removed and links are updated
C. All nodes are deleted regardless of 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 data matches the value to delete.
  2. Step 2: Update 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.
  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 fix links to remove it [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node without checking value
2. Which of the following is the correct syntax to delete a node by value in a doubly linked list in C?
easy
A. void deleteByValue(Node** head, int val) { /* code */ }
B. int deleteByValue(Node head, int val) { /* code */ }
C. void deleteByValue(Node* head, int val) { /* code */ }
D. Node deleteByValue(Node** head, int val) { /* code */ }

Solution

  1. Step 1: Understand pointer usage for head modification

    Since deleting a node may change the head pointer, we pass a pointer to the head pointer (Node** head).
  2. Step 2: Confirm function return type

    The function deletes a node and does not return a node or int, so return type is void.
  3. Final Answer:

    void deleteByValue(Node** head, int val) { /* code */ } -> Option A
  4. Quick Check:

    Use double pointer to modify head [OK]
Hint: Use Node** to allow head pointer changes [OK]
Common Mistakes:
  • Passing head as single pointer when head may change
  • Returning wrong type from delete function
  • Not using void when no return needed
3. What will be the output list after deleting value 3 from the doubly linked list: 1 <-> 3 <-> 5 <-> 3 <-> 7?
medium
A. 1 <-> 3 <-> 5 <-> 7
B. 3 <-> 5 <-> 3 <-> 7
C. 1 <-> 5 <-> 7
D. 1 <-> 5 <-> 3 <-> 7

Solution

  1. Step 1: Identify which node is deleted

    Deleting by value removes the first node with value 3, which is the second node in the list.
  2. Step 2: Update links and list after deletion

    After removing the first 3, the list becomes 1 <-> 5 <-> 3 <-> 7, keeping the second 3 intact.
  3. Final Answer:

    1 <-> 5 <-> 3 <-> 7 -> Option D
  4. Quick Check:

    Only first matching node deleted [OK]
Hint: Delete only first matching node by value [OK]
Common Mistakes:
  • Deleting all nodes with value 3 instead of first
  • Not updating links correctly after deletion
  • Deleting wrong node (like last 3 instead of first)
4. Given this code snippet to delete a node by value, what is the error?
void deleteByValue(Node** head, int val) {
  Node* temp = *head;
  while (temp != NULL && temp->data != val) {
    temp = temp->next;
  }
  if (temp == NULL) return;
  if (temp->prev != NULL) temp->prev->next = temp->next;
  if (temp->next != NULL) temp->next->prev = temp->prev;
  free(temp);
}
medium
A. Loops infinitely when value not found
B. Does not free memory of deleted node
C. Does not update head if first node is deleted
D. Deletes all nodes with matching value

Solution

  1. Step 1: Check head update when deleting first node

    If the node to delete is the head, the head pointer must be updated to the next node, but this code does not do that.
  2. Step 2: Confirm other parts are correct

    Memory is freed, loop ends correctly, and only one node is deleted, so no other errors.
  3. Final Answer:

    Does not update head if first node is deleted -> Option C
  4. Quick Check:

    Update head pointer when deleting first node [OK]
Hint: Always update head if deleting first node [OK]
Common Mistakes:
  • Forgetting to update head pointer
  • Not freeing memory after deletion
  • Incorrect loop causing infinite iteration
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. You want to delete the node with value 10 and then insert a new node with value 5 at the head. What will be the final list?
hard
A. 5 <-> 20 <-> 30 <-> 40
B. 10 <-> 5 <-> 20 <-> 30 <-> 40
C. 20 <-> 30 <-> 40 <-> 5
D. 5 <-> 10 <-> 20 <-> 30 <-> 40

Solution

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

    Removing the head node 10 updates the head pointer to node 20, so list becomes 20 <-> 30 <-> 40.
  2. Step 2: Insert new node with value 5 at head

    Create a new node with value 5, set its next to current head (20), update 20's prev to new node, and update head pointer to new node.
  3. Step 3: Final list after operations

    The list is now 5 <-> 20 <-> 30 <-> 40.
  4. Final Answer:

    5 <-> 20 <-> 30 <-> 40 -> Option A
  5. Quick Check:

    Delete head, then insert new head [OK]
Hint: Delete head, then insert new node at head [OK]
Common Mistakes:
  • Not updating head after deletion
  • Not linking new node correctly at head
  • Appending new node at tail instead of head
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