Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Delete Node by Value in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting a node by value in a singly linked list?
Consider a singly linked list with nodes containing values 1, 2, 3, 4, 5 in order. We delete the node with value 3. What is the printed linked list after deletion?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  deleteByValue(value) {
    if (!this.head) return;
    if (this.head.value === value) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    while (current.next && current.next.value !== value) {
      current = current.next;
    }
    if (current.next && current.next.value === value) {
      current.next = current.next.next;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new LinkedList();
[1, 2, 3, 4, 5].forEach(v => list.append(v));
list.deleteByValue(3);
list.printList();
A1 -> 2 -> 3 -> 4 -> 5 -> null
B2 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 4 -> 3 -> 5 -> null
D1 -> 2 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember to unlink the node with the target value and connect the previous node to the next node.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting a non-existent value from a singly linked list?
Given a singly linked list with values 10, 20, 30, 40, 50, what is the output after attempting to delete the value 60 (which is not in the list)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  deleteByValue(value) {
    if (!this.head) return;
    if (this.head.value === value) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    while (current.next && current.next.value !== value) {
      current = current.next;
    }
    if (current.next && current.next.value === value) {
      current.next = current.next.next;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new LinkedList();
[10, 20, 30, 40, 50].forEach(v => list.append(v));
list.deleteByValue(60);
list.printList();
A10 -> 20 -> 30 -> 40 -> 50 -> null
B20 -> 30 -> 40 -> 50 -> null
Cnull
D10 -> 20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
🔧 Debug
advanced
2:00remaining
Why does this deleteByValue method fail to delete the last node?
The following code tries to delete a node by value from a singly linked list. It works for all nodes except the last one. What is the bug?
DSA Javascript
deleteByValue(value) {
  let current = this.head;
  while (current.next.value !== value) {
    current = current.next;
  }
  current.next = current.next.next;
}
AIt does not check if current.next is null before accessing current.next.value, causing an error if value is last node.
BIt uses current.next.next without checking if current.next.next exists, causing error if deleting second last node.
CIt should update this.head when deleting the first node but does not.
DIt uses a while loop without a break, causing infinite loop.
Attempts:
2 left
💡 Hint
Check for null before accessing properties of next node.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting the head node by value?
Given a singly linked list with values 7, 14, 21, 28, what is the printed list after deleting the node with value 7?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  deleteByValue(value) {
    if (!this.head) return;
    if (this.head.value === value) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    while (current.next && current.next.value !== value) {
      current = current.next;
    }
    if (current.next && current.next.value === value) {
      current.next = current.next.next;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new LinkedList();
[7, 14, 21, 28].forEach(v => list.append(v));
list.deleteByValue(7);
list.printList();
A21 -> 28 -> null
B7 -> 14 -> 21 -> 28 -> null
C14 -> 21 -> 28 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head means moving the head pointer to the next node.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting a node by value in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting a node by value in the worst case?
AO(n^2) because of nested loops in deletion
BO(n) because we may need to traverse the entire list to find the node
CO(log n) because of binary search on the list
DO(1) because deletion is just pointer update
Attempts:
2 left
💡 Hint
Think about how many nodes you might need to check before finding the target.

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