Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Delete Node at Specific Position 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 node at position 2?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what is the list after deleting the node at position 2 (0-based index)?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

  deleteAtPosition(pos) {
    if (!this.head) return;
    if (pos === 0) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    let prev = null;
    let count = 0;
    while (current && count < pos) {
      prev = current;
      current = current.next;
      count++;
    }
    if (current) {
      prev.next = current.next;
    }
  }

  toArray() {
    const result = [];
    let current = this.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }
    return result;
  }
}

const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.append(40);
list.deleteAtPosition(2);
console.log(list.toArray());
A[10, 20, 40]
B[10, 30, 40]
C[20, 30, 40]
D[10, 20, 30, 40]
Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list (0-based).
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting node at position 0?
Given the linked list 5 -> 15 -> 25 -> null, what is the list after deleting the node at position 0?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

  deleteAtPosition(pos) {
    if (!this.head) return;
    if (pos === 0) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    let prev = null;
    let count = 0;
    while (current && count < pos) {
      prev = current;
      current = current.next;
      count++;
    }
    if (current) {
      prev.next = current.next;
    }
  }

  toArray() {
    const result = [];
    let current = this.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }
    return result;
  }
}

const list = new LinkedList();
list.append(5);
list.append(15);
list.append(25);
list.deleteAtPosition(0);
console.log(list.toArray());
A[15, 25]
B[5, 25]
C[5, 15]
D[25]
Attempts:
2 left
💡 Hint
Deleting position 0 removes the first node.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting node at position 4?
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, what is the list after deleting the node at position 4?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

  deleteAtPosition(pos) {
    if (!this.head) return;
    if (pos === 0) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    let prev = null;
    let count = 0;
    while (current && count < pos) {
      prev = current;
      current = current.next;
      count++;
    }
    if (current) {
      prev.next = current.next;
    }
  }

  toArray() {
    const result = [];
    let current = this.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }
    return result;
  }
}

const list = new LinkedList();
[1, 2, 3, 4, 5].forEach(n => list.append(n));
list.deleteAtPosition(4);
console.log(list.toArray());
A[2, 3, 4, 5]
B[1, 2, 3, 5]
C[1, 2, 3, 4]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Position 4 is the last node in this list.
❓ Predict Output
advanced
2:00remaining
What happens if you delete node at position 10 in a 5-node list?
Given the linked list 100 -> 200 -> 300 -> 400 -> 500 -> null, what is the list after deleting the node at position 10?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

  deleteAtPosition(pos) {
    if (!this.head) return;
    if (pos === 0) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    let prev = null;
    let count = 0;
    while (current && count < pos) {
      prev = current;
      current = current.next;
      count++;
    }
    if (current) {
      prev.next = current.next;
    }
  }

  toArray() {
    const result = [];
    let current = this.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }
    return result;
  }
}

const list = new LinkedList();
[100, 200, 300, 400, 500].forEach(n => list.append(n));
list.deleteAtPosition(10);
console.log(list.toArray());
ATypeError
B[200, 300, 400, 500]
C[100, 200, 300, 400, 500]
D[100, 200, 300, 400]
Attempts:
2 left
💡 Hint
Deleting a position beyond list length should not change the list.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting a node at position k in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity to delete the node at position k (0-based index)?
AO(1) because deletion is always constant time
BO(n) because you must traverse the entire list always
CO(n-k) because you traverse from the end backwards
DO(k) because you must traverse to the node before position k
Attempts:
2 left
💡 Hint
Think about how you reach the node before the one to delete.

Practice

(1/5)
1. What happens when you delete a node at position 0 in a singly linked list?
easy
A. The head pointer moves to the next node, effectively removing the first node.
B. The last node is removed from the list.
C. The list becomes empty regardless of its size.
D. The node at position 1 is deleted instead.

Solution

  1. Step 1: Understand position 0 in linked list

    Position 0 refers to the first node, also called the head.
  2. Step 2: Effect of deleting head node

    Deleting the head means moving the head pointer to the next node, removing the first node from the list.
  3. Final Answer:

    The head pointer moves to the next node, effectively removing the first node. -> Option A
  4. Quick Check:

    Deleting position 0 removes the first node [OK]
Hint: Deleting position 0 means changing the head pointer [OK]
Common Mistakes:
  • Thinking the last node is deleted instead
  • Assuming the list becomes empty always
  • Deleting the wrong node position
2. Which of the following is the correct syntax to delete a node at position pos in a singly linked list in JavaScript?
easy
A. head = head[pos + 1];
B. if (pos === 0) head = head.next;
C. delete head[pos];
D. head.next = null;

Solution

  1. Step 1: Check deletion at position 0

    Deleting at position 0 means moving head to head.next.
  2. Step 2: Validate syntax correctness

    if (pos === 0) head = head.next; correctly assigns head to head.next. Other options misuse syntax or do not delete the node properly.
  3. Final Answer:

    if (pos === 0) head = head.next; -> Option B
  4. Quick Check:

    Correct syntax for deleting head node is head = head.next [OK]
Hint: To delete head, assign head = head.next [OK]
Common Mistakes:
  • Using delete keyword incorrectly on linked list nodes
  • Assigning head.next to null without updating head
  • Accessing nodes with array-like syntax
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> null, what is the list after deleting the node at position 2?
medium
A. 10 -> 20 -> 30 -> null
B. 10 -> 30 -> 40 -> null
C. 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 40 -> null

Solution

  1. Step 1: Identify node at position 2

    Positions start at 0, so position 2 is the third node with value 30.
  2. Step 2: Delete node at position 2

    Remove node 30 by linking node 20's next to node 40.
  3. Final Answer:

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

    Deleted node 30 at position 2 [OK]
Hint: Count from 0, remove node at that index [OK]
Common Mistakes:
  • Deleting wrong node by miscounting positions
  • Not updating the previous node's next pointer
  • Confusing node values with positions
4. What is the error in the following code snippet for deleting a node at position pos in a singly linked list?
function deleteNode(head, pos) {
  let current = head;
  for (let i = 0; i < pos - 1; i++) {
    current = current.next;
  }
  current.next = current.next.next;
  return head;
}
medium
A. It does not handle deletion when pos is 0 (head node).
B. It incorrectly updates the head pointer.
C. It causes a syntax error due to missing semicolons.
D. It deletes the last node instead of the specified position.

Solution

  1. Step 1: Analyze loop for position traversal

    The loop stops at node before position to delete, which is correct for pos > 0.
  2. Step 2: Check handling of pos = 0

    When pos is 0, the loop does not run and current is head, but code does not update head pointer, so deletion fails.
  3. Final Answer:

    It does not handle deletion when pos is 0 (head node). -> Option A
  4. Quick Check:

    Missing head deletion case causes error [OK]
Hint: Always check if deleting head node separately [OK]
Common Mistakes:
  • Ignoring edge case when pos is 0
  • Assuming loop handles all positions
  • Not updating head pointer when needed
5. You have a singly linked list: 5 -> 15 -> 25 -> 35 -> 45 -> null. You want to delete the node at position 4. What will the list look like after deletion? Also, what happens if you try to delete at position 5?
hard
A. After deletion at position 4: 5 -> 15 -> 25 -> 35 -> null; Deleting position 5 causes a runtime error.
B. After deletion at position 4: 5 -> 15 -> 25 -> 35 -> 45 -> null; Deleting position 5 removes the last node.
C. After deletion at position 4: 5 -> 15 -> 25 -> 35 -> null; Deleting position 5 does nothing as it's out of range.
D. After deletion at position 4: 15 -> 25 -> 35 -> 45 -> null; Deleting position 5 removes the head.

Solution

  1. Step 1: Identify node at position 4

    Positions start at 0, so position 4 is the fifth node with value 45.
  2. Step 2: Delete node at position 4

    Remove node 45 by linking node 35's next to null.
  3. Step 3: Consider deletion at position 5

    Position 5 is out of list range (list length is 5), so deletion does nothing safely.
  4. Final Answer:

    After deletion at position 4: 5 -> 15 -> 25 -> 35 -> null; Deleting position 5 does nothing as it's out of range. -> Option C
  5. Quick Check:

    Deleting last node removes it; out-of-range deletion ignored [OK]
Hint: Deleting out-of-range position does nothing [OK]
Common Mistakes:
  • Trying to delete non-existent node causing errors
  • Confusing positions and node values
  • Not updating previous node's next pointer correctly
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