Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Delete by Value in Doubly Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly 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 middle value?
Consider a doubly linked list with nodes 1 <-> 2 <-> 3 <-> 4. We delete the node with value 3. What is the printed list after deletion?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteByValue(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        if (current.prev) {
          current.prev.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev;
        }
        return;
      }
      current = current.next;
    }
  }

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

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.deleteByValue(3);
dll.printList();
A1 -> 2 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C1 -> 3 -> 4 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about how the links between nodes change when you remove a middle node.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting the head node?
Given a doubly linked list 10 <-> 20 <-> 30, if we delete the node with value 10 (the head), what is the printed list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteByValue(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        if (current.prev) {
          current.prev.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev;
        }
        return;
      }
      current = current.next;
    }
  }

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

const dll = new DoublyLinkedList();
dll.append(10);
dll.append(20);
dll.append(30);
dll.deleteByValue(10);
dll.printList();
Anull
B20 -> 30 -> null
C30 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting the tail node?
For a doubly linked list 5 <-> 15 <-> 25, if we delete the node with value 25 (the tail), what is the printed list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteByValue(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        if (current.prev) {
          current.prev.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev;
        }
        return;
      }
      current = current.next;
    }
  }

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

const dll = new DoublyLinkedList();
dll.append(5);
dll.append(15);
dll.append(25);
dll.deleteByValue(25);
dll.printList();
A5 -> 15 -> null
B5 -> 15 -> 25 -> null
C15 -> 25 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the tail means the previous node becomes the new tail.
❓ Predict Output
advanced
2:00remaining
What is the output when deleting a non-existent value?
Given a doubly linked list 7 <-> 14 <-> 21, if we try to delete the node with value 100 (which does not exist), what is the printed list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

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

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteByValue(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        if (current.prev) {
          current.prev.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev;
        }
        return;
      }
      current = current.next;
    }
  }

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

const dll = new DoublyLinkedList();
dll.append(7);
dll.append(14);
dll.append(21);
dll.deleteByValue(100);
dll.printList();
A7 -> 21 -> null
B14 -> 21 -> null
C7 -> 14 -> 21 -> null
Dnull
Attempts:
2 left
💡 Hint
If the value is not found, the list stays the same.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting a node by value in a doubly linked list?
Consider a doubly linked list with n nodes. What is the time complexity of deleting a node by its value when you do not have a direct reference to the node?
AO(1) because deletion is always constant time in doubly linked lists
BO(n^2) because you must check each node multiple times
CO(log n) because you can use binary search to find the node
DO(n) because you may need to traverse the entire list to find the node
Attempts:
2 left
💡 Hint
Think about how you find the node before deleting it.

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