Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Delete Node by Value in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node by Value
Start at head
↓
Check if head is null?
Yes→List empty, stop
No↓
Check if head value == target?
Yes→Remove head, update head
No↓
Traverse list with prev and current
↓
Check current value == target?
Yes→Remove current by linking prev to current.next
No↓
Move prev and current forward
Back to check current value↓
Reached end without finding target
↓
Stop
↩Back to check current value
Start from the head, check if the node to delete is the head, else traverse the list to find and remove the node by updating links.
Execution Sample
DSA Javascript
function deleteNode(head, value) {
  if (!head) return null;
  if (head.val === value) return head.next;
  let prev = head;
  let current = head.next;
  while (current) {
    if (current.val === value) { prev.next = current.next; break; }
    prev = current;
    current = current.next;
  }
  return head;
}
Deletes the first node with the given value from a singly linked list.
Execution Table
StepActionCurrent Node ValuePrev Node ValueConditionList State
1Check if head is null1nullhead != null1 -> 2 -> 3 -> 4 -> null
2Check if head.val == value (3)1null1 == 3? No1 -> 2 -> 3 -> 4 -> null
3Set prev = head, current = head.next21-1 -> 2 -> 3 -> 4 -> null
4Check if current.val == value (3)212 == 3? No1 -> 2 -> 3 -> 4 -> null
5Move prev and current forward32-1 -> 2 -> 3 -> 4 -> null
6Check if current.val == value (3)323 == 3? Yes1 -> 2 -> 3 -> 4 -> null
7Remove current by linking prev.next = current.next32-1 -> 2 -> 4 -> null
8Break loop and return head32-1 -> 2 -> 4 -> null
💡 Node with value 3 found and deleted, loop breaks and function returns updated list.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
headNode(1)Node(1)Node(1)Node(1)Node(1)
prevnullNode(1)Node(2)Node(2)Node(2)
currentNode(1)Node(2)Node(3)Node(3)Node(3)
Key Moments - 3 Insights
Why do we check if the head node contains the value before traversing?
Because if the head node has the value, we remove it by returning head.next immediately, avoiding unnecessary traversal. See execution_table step 2.
What happens if the value is not found in the list?
The loop finishes without changing links, and the original list is returned unchanged. This is implied after step 8 where no deletion occurs if no match.
Why do we keep track of both prev and current nodes?
We need prev to update its next pointer to skip the current node when deleting. Without prev, we can't link around the node to remove. See steps 3-7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the value of current.val?
A2
B3
C4
D1
💡 Hint
Check the 'Current Node Value' column at step 6 in the execution_table.
At which step does the list change by removing a node?
AStep 4
BStep 6
CStep 7
DStep 8
💡 Hint
Look for the step where prev.next is updated to skip the current node in the execution_table.
If the value to delete was 1 (head node), what would happen at step 2?
AThe function returns head.next immediately
BThe function starts traversing the list
CThe function returns null
DThe function deletes the last node
💡 Hint
Refer to the condition check at step 2 and what happens if head.val equals the target value.
Concept Snapshot
Delete Node by Value in Singly Linked List:
- Check if list is empty (head null) -> return null
- If head node matches value, remove by returning head.next
- Else traverse with prev and current pointers
- When current.val matches, link prev.next to current.next
- Return head of updated list
Full Transcript
This visual trace shows how to delete a node by value in a singly linked list. We start by checking if the list is empty. If not, we check if the head node contains the value to delete. If yes, we remove the head by returning the next node. Otherwise, we traverse the list with two pointers: prev and current. When we find the node with the target value, we remove it by linking the previous node's next pointer to skip the current node. The function then returns the updated list head. Variables head, prev, and current change as we move through the list. Key moments include handling deletion at the head, why prev is needed, and what happens if the value is not found. The quizzes test understanding of node values at steps, when deletion happens, and behavior when deleting the head node.

Practice

(1/5)
1. What is the main purpose of the deleteNodeByValue function in a linked list?
easy
A. To reverse the linked list
B. To add a new node with the given value at the end of the list
C. To find the position of a node with the given value
D. To remove the first node that contains the given value from the list

Solution

  1. Step 1: Understand the function's goal

    The function aims to remove a node that matches a specific value from the linked list.
  2. Step 2: Identify the action performed

    Deleting a node by value means finding the first node with that value and removing it from the list.
  3. Final Answer:

    To remove the first node that contains the given value from the list -> Option D
  4. Quick Check:

    Delete node by value = Remove node with value [OK]
Hint: Deleting by value means removing matching node, not adding [OK]
Common Mistakes:
  • Confusing deletion with insertion
  • Thinking it finds position only
  • Assuming it reverses the list
2. Which of the following is the correct way to check if the head node contains the value to delete in JavaScript?
easy
A. if (head.value === value) { /* delete head */ }
B. if (head == value) { /* delete head */ }
C. if (head.value = value) { /* delete head */ }
D. if (head.value != value) { /* delete head */ }

Solution

  1. Step 1: Understand comparison syntax in JavaScript

    Use triple equals (===) to compare values strictly without type coercion.
  2. Step 2: Identify correct condition for head node value check

    Check if head.value equals the target value using ===.
  3. Final Answer:

    if (head.value === value) { /* delete head */ } -> Option A
  4. Quick Check:

    Use === for value comparison [OK]
Hint: Use === to compare values, not = or == [OK]
Common Mistakes:
  • Using single = which assigns instead of compares
  • Using == which can cause unexpected type coercion
  • Comparing head node directly to value without .value
3. Consider the linked list: 10 -> 20 -> 30 -> 40 -> null. After calling deleteNodeByValue(head, 30), what will be the new list?
medium
A. 20 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 40 -> null
D. 10 -> 30 -> 40 -> null

Solution

  1. Step 1: Locate the node with value 30

    Traverse the list: 10, 20, then 30 is found as the third node.
  2. Step 2: Remove the node with value 30

    Adjust the previous node's next pointer (20's next) to point to 40, skipping 30.
  3. Final Answer:

    10 -> 20 -> 40 -> null -> Option C
  4. Quick Check:

    Deleted 30 node, list skips it [OK]
Hint: Skip node by linking previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node or none
  • Confusing node values or order
4. Given this code snippet to delete a node by value, what is the main bug?
function deleteNodeByValue(head, value) {
  let current = head;
  while (current.next !== null) {
    if (current.next.value === value) {
      current.next = current.next.next;
      return head;
    }
    current = current.next;
  }
  return head;
}
medium
A. It causes an infinite loop when value is not found
B. It does not handle deleting the head node if it matches the value
C. It deletes all nodes with the given value instead of just one
D. It throws an error if the list is empty

Solution

  1. Step 1: Analyze head node deletion case

    The code starts checking from current.next, so it never checks if head itself has the value.
  2. Step 2: Identify missing head deletion handling

    If the head node contains the value, it won't be deleted, causing incorrect behavior.
  3. Final Answer:

    It does not handle deleting the head node if it matches the value -> Option B
  4. Quick Check:

    Head node deletion missing = Bug [OK]
Hint: Always check head node separately before loop [OK]
Common Mistakes:
  • Assuming loop covers head node
  • Ignoring empty list case
  • Thinking it deletes all matching nodes
5. You have a singly linked list: 5 -> 10 -> 15 -> 10 -> 20 -> null. You want to delete the first node with value 10. After deletion, what is the correct list?
hard
A. 5 -> 15 -> 10 -> 20 -> null
B. 5 -> 10 -> 15 -> 20 -> null
C. 5 -> 15 -> 20 -> null
D. 10 -> 15 -> 10 -> 20 -> null

Solution

  1. Step 1: Identify the first node with value 10

    Traverse from head: 5 (no), then 10 (yes) at second node.
  2. Step 2: Delete the first 10 node

    Link 5's next pointer to 15, skipping the first 10 node.
  3. Final Answer:

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

    Only first 10 removed, rest remain [OK]
Hint: Delete first matching node only, keep others [OK]
Common Mistakes:
  • Deleting all nodes with value 10
  • Deleting wrong 10 node
  • Breaking list links incorrectly
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