Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node 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 node by value
What is the printed linked list after deleting the node with value 3?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  append(value: number) {
    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: number) {
    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();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.deleteByValue(3);
list.printList();
A1 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C2 -> 3 -> 4 -> null
D1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node by value
What is the printed linked list after deleting the node with value 1 (head node)?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  append(value: number) {
    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: number) {
    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();
list.append(1);
list.append(2);
list.append(3);
list.deleteByValue(1);
list.printList();
A3 -> null
B1 -> 2 -> 3 -> null
C2 -> 3 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer moves to the next node.
🔧 Debug
advanced
2:00remaining
Identify the bug preventing deletion of head node in deleteByValue
This deleteByValue method has a bug when deleting the head node. What is the bug?
DSA Typescript
deleteByValue(value: number) {
  if (!this.head) 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;
  }
}
AThe method does not update the tail pointer after deletion
BMissing check for deleting head node causes failure to delete if head matches value
CThe loop condition should be current != null instead of current.next != null
DThe method should use recursion instead of a loop
Attempts:
2 left
💡 Hint
Consider what happens if the node to delete is the first node.
🧠 Conceptual
advanced
1:30remaining
Effect of deleting a non-existent value
What happens to the linked list if deleteByValue is called with a value not present in the list?
AThe list remains unchanged because no node matches the value
BThe method throws an error
CThe head node is deleted regardless
DThe last node is deleted by mistake
Attempts:
2 left
💡 Hint
Think about how the method searches for the node to delete.
🚀 Application
expert
3:00remaining
Resulting list after multiple deletions by value
Given the initial list 5 -> 10 -> 15 -> 20 -> 25 -> null, what is the list after deleting nodes with values 10, 25, and 5 in that order?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  append(value: number) {
    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: number) {
    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();
[5, 10, 15, 20, 25].forEach(v => list.append(v));
list.deleteByValue(10);
list.deleteByValue(25);
list.deleteByValue(5);
list.printList();
A15 -> 20 -> null
B5 -> 15 -> 20 -> null
C15 -> 20 -> 25 -> null
D10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Delete nodes one by one and update the list each time.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a linked list?
easy
A. Reverse the linked list
B. Add a new node with the given value at the end
C. Remove the first node that contains the given value
D. Find the position of a node with the given value

Solution

  1. Step 1: Understand the operation goal

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

    Removing the first node with the given value means deleting that node and linking its previous node to the next node.
  3. Final Answer:

    Remove the first node that contains the given value -> Option C
  4. Quick Check:

    Delete node by value = Remove first matching node [OK]
Hint: Delete node by value means remove first matching node [OK]
Common Mistakes:
  • Confusing deletion with insertion
  • Trying to delete all nodes with the value instead of first
  • Thinking it finds position only
  • Assuming it reverses the list
2. Which of the following TypeScript code snippets correctly deletes the head node if its value is 5 from a singly linked list starting at head?
easy
A. if (head.value === 5) head = head.next;
B. head = head.next.value === 5 ? head.next.next : head;
C. while (head.value !== 5) head = head.next;
D. head.next = null;

Solution

  1. Step 1: Check if head node has value 5

    If the first node's value is 5, we remove it by moving head to the next node.
  2. Step 2: Verify code correctness

    if (head.value === 5) head = head.next; correctly updates head to head.next if head.value is 5, effectively deleting the first node.
  3. Final Answer:

    if (head.value === 5) head = head.next; -> Option A
  4. Quick Check:

    Delete head node by reassigning head [OK]
Hint: Check head value first, then update head pointer [OK]
Common Mistakes:
  • Using incorrect conditional expressions
  • Assigning head.next incorrectly
  • Not handling head node deletion
  • Overwriting head without checks
3. Given the linked list: 1 -> 3 -> 5 -> 7 -> null, what is the list after deleting the node with value 5 using the following TypeScript code?
let current = head;
while (current.next !== null) {
  if (current.next.value === 5) {
    current.next = current.next.next;
    break;
  }
  current = current.next;
}
medium
A. 1 -> 3 -> 5 -> 7 -> null
B. 1 -> 3 -> 7 -> null
C. 3 -> 5 -> 7 -> null
D. 1 -> 7 -> null

Solution

  1. Step 1: Traverse list to find node before value 5

    Loop checks nodes until current.next.value is 5, which is true when current is node with value 3.
  2. Step 2: Delete node with value 5 by skipping it

    Set current.next to current.next.next, linking node 3 directly to node 7, removing node 5.
  3. Final Answer:

    1 -> 3 -> 7 -> null -> Option B
  4. Quick Check:

    Skip node with value 5 = 1->3->7 [OK]
Hint: Skip node by linking previous to next [OK]
Common Mistakes:
  • Not breaking loop after deletion
  • Deleting wrong node
  • Not updating pointers correctly
  • Confusing current and current.next
4. Consider this buggy TypeScript code to delete a node by value from a singly linked list:
function deleteNode(head: Node | null, value: number): Node | null {
  let current = head;
  while (current !== null) {
    if (current.next?.value === value) {
      current.next = current.next.next;
    }
    current = current.next!;
  }
  return head;
}
What is the main problem with this code?
medium
A. It may cause a runtime error when current.next is null
B. It deletes all nodes with the value instead of the first
C. It does not handle deletion if head node has the value
D. It reverses the linked list accidentally

Solution

  1. Step 1: Analyze the loop termination

    The while (current !== null) continues to the last node where current.next is null. The if uses ?. safely, but then executes current = current.next!;, and ! non-null assertion on null causes runtime error.
  2. Step 2: Confirm the bug triggers always

    For any non-empty list, it crashes at the last node regardless of value, preventing correct execution.
  3. Final Answer:

    It may cause a runtime error when current.next is null -> Option A
  4. Quick Check:

    current.next! on null = runtime error [OK]
Hint: Beware of non-null assertion ! on potentially null next [OK]
Common Mistakes:
  • Using non-null assertion (!) on potentially null next
  • Assuming current.next always exists
  • Not breaking after deletion
  • Confusing current and current.next updates
5. You have a singly linked list: 2 -> 4 -> 6 -> 4 -> 8 -> null. You want to delete the first node with value 4. After deletion, you want to insert a new node with value 10 right after the deleted node's position. Which sequence of operations correctly achieves this?
hard
A. Delete first 4, then insert 10 after node 6
B. Insert 10 at head, then delete first 4
C. Delete all 4s, then insert 10 at end
D. Delete first 4, then insert 10 after node 2

Solution

  1. Step 1: Delete first node with value 4

    The first 4 is the second node, so remove it by linking node 2 to node 6.
  2. Step 2: Insert new node with value 10 after node 2

    Create new node 10 and link it after node 2, then link new node to node 6.
  3. Final Answer:

    Delete first 4, then insert 10 after node 2 -> Option D
  4. Quick Check:

    Delete 4, insert 10 after previous node [OK]
Hint: Delete node, then insert new node at same position [OK]
Common Mistakes:
  • Inserting new node at wrong position
  • Deleting all nodes with value 4
  • Not updating links correctly
  • Inserting before deletion
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