Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Delete by Value in Doubly Linked List in DSA Typescript - 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
Output after deleting a middle value
What is the printed state of the doubly linked list after deleting the value 3?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class DoublyLinkedList {
  head: Node | null = null;
  tail: Node | null = null;

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

  deleteByValue(value: number) {
    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;
        }
        break;
      }
      current = current.next;
    }
  }

  printList() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' -> ') + ' -> null');
  }
}

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.deleteByValue(3);
dll.printList();
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 4 -> 5 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 5 -> null
Attempts:
2 left
💡 Hint
Think about how the links between nodes change when you remove a node in the middle.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class DoublyLinkedList {
  head: Node | null = null;
  tail: Node | null = null;

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

  deleteByValue(value: number) {
    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;
        }
        break;
      }
      current = current.next;
    }
  }

  printList() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' -> ') + ' -> null');
  }
}

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.deleteByValue(1);
dll.printList();
A1 -> 2 -> 3 -> 4 -> 5 -> null
B2 -> 3 -> 4 -> null
C1 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
🔧 Debug
advanced
2:30remaining
Identify the bug causing incorrect tail update
After deleting the last node by value, the tail pointer is not updated correctly. Which option shows the cause of this bug?
DSA Typescript
deleteByValue(value: number) {
  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;
      }
      // Missing else block here
      break;
    }
    current = current.next;
  }
}
AIncorrectly setting head to current.prev instead of current.next
BNot breaking the loop after deletion
CMissing else block to update tail when current.next is null
DNot updating current.prev.next when current.prev is null
Attempts:
2 left
💡 Hint
Think about what happens when deleting the last node and how tail should be updated.
❓ Predict Output
advanced
2:00remaining
Output after deleting a non-existent value
What is the printed state of the doubly linked list after attempting to delete the value 10 which is not in the list?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class DoublyLinkedList {
  head: Node | null = null;
  tail: Node | null = null;

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

  deleteByValue(value: number) {
    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;
        }
        break;
      }
      current = current.next;
    }
  }

  printList() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' -> ') + ' -> null');
  }
}

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.deleteByValue(10);
dll.printList();
A1 -> 2 -> 3 -> 4 -> 5 -> null
Bnull
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
🧠 Conceptual
expert
3:00remaining
Why is updating both prev and next pointers necessary when deleting a node?
In a doubly linked list, when deleting a node by value, why must we update both the previous node's next pointer and the next node's prev pointer?
ATo maintain the bidirectional links so traversal in both directions remains correct
BOnly the previous node's next pointer needs updating because the list is singly linked
CUpdating the next node's prev pointer is optional and only needed for circular lists
DBecause the head and tail pointers automatically adjust without updating node links
Attempts:
2 left
💡 Hint
Think about how nodes connect forward and backward in a doubly linked list.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. All nodes are deleted regardless of value
B. The node with the matching value is removed and links are updated
C. Only the first node is deleted no matter the value
D. The list is reversed instead of deleting

Solution

  1. Step 1: Identify 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 exclude the found node.
  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 update previous and next links [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting all nodes instead of one
2. Which of the following is the correct TypeScript syntax to delete a node by value in a doubly linked list?
easy
A. function deleteNode(value) { /* code */ }
B. removeNode(value: number) => { /* code */ }
C. deleteNode(value: number): void { /* code */ }
D. delete node(value: number) { /* code */ }

Solution

  1. Step 1: Check method declaration syntax in TypeScript class

    Methods inside classes use the syntax methodName(params): returnType { body }.
  2. Step 2: Identify correct syntax for deleteNode method

    deleteNode(value: number): void { /* code */ } uses correct TypeScript method syntax with type annotation and void return.
  3. Final Answer:

    deleteNode(value: number): void { /* code */ } -> Option C
  4. Quick Check:

    TypeScript method syntax = deleteNode(value: number): void { /* code */ } [OK]
Hint: Use methodName(params): returnType { } inside class [OK]
Common Mistakes:
  • Using arrow function syntax inside class method
  • Missing type annotations
  • Using invalid keywords like 'delete node'
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> 40, what is the list after deleting the node with value 30?
medium
A. 10 <-> 20 <-> 40
B. 10 <-> 30 <-> 40
C. 20 <-> 30 <-> 40
D. 10 <-> 20 <-> 30 <-> 40

Solution

  1. Step 1: Locate node with value 30

    Traverse the list: 10 -> 20 -> 30 found at third node.
  2. Step 2: Remove node 30 and link 20 to 40

    Update 20's next pointer to 40 and 40's previous pointer to 20.
  3. Final Answer:

    10 <-> 20 <-> 40 -> Option A
  4. Quick Check:

    Deleted 30, list links updated [OK]
Hint: Remove node, link previous and next nodes directly [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node
4. What is wrong with this delete function snippet for doubly linked list?
deleteNode(value: number): void {
  let current = this.head;
  while (current !== null) {
    if (current.value === value) {
      current.prev.next = current.next;
      current.next.prev = current.prev;
      break;
    }
    current = current.next;
  }
}
medium
A. It does not handle deleting the head node correctly
B. It deletes all nodes with the value instead of one
C. It uses wrong variable names
D. It causes infinite loop

Solution

  1. Step 1: Check handling of head node deletion

    If current is head, current.prev is null, so current.prev.next causes error.
  2. Step 2: Identify missing head update

    Function does not update this.head when deleting the first node, causing broken list.
  3. Final Answer:

    It does not handle deleting the head node correctly -> Option A
  4. Quick Check:

    Head deletion 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 pointer 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, then delete the node with value 25. What is the final list?
hard
A. 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20
C. 10 <-> 15 <-> 20 <-> 25
D. 10 <-> 15 <-> 20

Solution

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

    Update head to 10, set 10.prev to null.
  2. Step 2: Delete node with value 25 (tail node)

    Update 20.next to null, removing 25.
  3. Final Answer:

    10 <-> 15 <-> 20 -> Option D
  4. Quick Check:

    Deleted head and tail nodes correctly [OK]
Hint: Update head or tail pointers when deleting first or last nodes [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating tail when deleting last node
  • Breaking links between middle 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