Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Delete Node at Specific Position in DSA Typescript - 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
Output after deleting node at position 3 in a singly linked list
Given the linked list 10 -> 20 -> 30 -> 40 -> 50 -> null, what is the output after deleting the node at position 3?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function deleteNodeAtPosition(head: Node | null, position: number): Node | null {
  if (!head) return null;
  if (position === 1) return head.next;
  let current = head;
  for (let i = 1; current !== null && i < position - 1; i++) {
    current = current.next;
  }
  if (current === null || current.next === null) return head;
  current.next = current.next.next;
  return head;
}

// Create list
const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);

const newHead = deleteNodeAtPosition(head, 3);

// Function to print list
function printList(head: Node | null): string {
  let result = '';
  let current = head;
  while (current) {
    result += current.value + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(newHead));
A20 -> 30 -> 40 -> 50 -> null
B10 -> 20 -> 30 -> 50 -> null
C10 -> 30 -> 40 -> 50 -> null
D10 -> 20 -> 40 -> 50 -> null
Attempts:
2 left
💡 Hint
Remember that position 1 means the head node. Deleting at position 3 removes the third node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 1 (head) in a singly linked list
Given the linked list 5 -> 15 -> 25 -> null, what is the output after deleting the node at position 1?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function deleteNodeAtPosition(head: Node | null, position: number): Node | null {
  if (!head) return null;
  if (position === 1) return head.next;
  let current = head;
  for (let i = 1; current !== null && i < position - 1; i++) {
    current = current.next;
  }
  if (current === null || current.next === null) return head;
  current.next = current.next.next;
  return head;
}

// Create list
const head = new Node(5);
head.next = new Node(15);
head.next.next = new Node(25);

const newHead = deleteNodeAtPosition(head, 1);

// Function to print list
function printList(head: Node | null): string {
  let result = '';
  let current = head;
  while (current) {
    result += current.value + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(newHead));
A15 -> 25 -> null
B5 -> 25 -> null
C5 -> 15 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting position 1 means removing the head node.
🔧 Debug
advanced
2:00remaining
Identify the error in this delete node function
What error will this TypeScript code raise when deleting a node at position 4 in a list of length 3?
DSA Typescript
function deleteNodeAtPosition(head: Node | null, position: number): Node | null {
  if (!head) return null;
  if (position === 1) return head.next;
  let current = head;
  for (let i = 1; i < position - 1; i++) {
    current = current.next!;
  }
  current.next = current.next!.next;
  return head;
}
ASyntaxError: Unexpected token '!'
BTypeError: Cannot read property 'next' of null
CNo error, deletion succeeds
DReferenceError: current is not defined
Attempts:
2 left
💡 Hint
Consider what happens when position is greater than the list length.
🧠 Conceptual
advanced
2:00remaining
Effect of deleting a node at a position greater than list length
What happens if you try to delete a node at position 10 in a singly linked list of length 5 using a standard deleteNodeAtPosition function?
AThe list remains unchanged because position is invalid
BThe last node is deleted instead
CThe function throws an error and stops execution
DAll nodes after position 5 are deleted
Attempts:
2 left
💡 Hint
Think about how the function checks for position validity.
🚀 Application
expert
3:00remaining
Resulting list after multiple deletions at specific positions
Starting with the list 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null, delete nodes at positions 2, then 4 (positions are relative to the current list after each deletion). What is the final list?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function deleteNodeAtPosition(head: Node | null, position: number): Node | null {
  if (!head) return null;
  if (position === 1) return head.next;
  let current = head;
  for (let i = 1; current !== null && i < position - 1; i++) {
    current = current.next;
  }
  if (current === null || current.next === null) return head;
  current.next = current.next.next;
  return head;
}

// Create list
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);

const afterFirstDeletion = deleteNodeAtPosition(head, 2);
const afterSecondDeletion = deleteNodeAtPosition(afterFirstDeletion, 4);

// Function to print list
function printList(head: Node | null): string {
  let result = '';
  let current = head;
  while (current) {
    result += current.value + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(afterSecondDeletion));
A1 -> 2 -> 3 -> 5 -> 6 -> null
B1 -> 3 -> 4 -> 5 -> 6 -> null
C1 -> 3 -> 4 -> 6 -> null
D1 -> 3 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint
Delete position 2 first, then delete position 4 on the updated list.

Practice

(1/5)
1. What does deleting a node at a specific position in a singly linked list mean?
easy
A. Removing the node located at that position and linking its previous node to the next node
B. Adding a new node at that position
C. Swapping the node at that position with the last node
D. Counting the total nodes in the list

Solution

  1. Step 1: Understand node deletion in linked list

    Deleting a node means removing it and connecting the previous node to the next node to keep the list intact.
  2. Step 2: Apply to specific position

    At a specific position, we find the node before it and change its next pointer to skip the node to delete.
  3. Final Answer:

    Removing the node located at that position and linking its previous node to the next node -> Option A
  4. Quick Check:

    Delete node = remove and link neighbors [OK]
Hint: Delete means remove and link neighbors, not add or swap [OK]
Common Mistakes:
  • Thinking deletion adds a node
  • Confusing deletion with swapping nodes
  • Ignoring the need to link previous and next nodes
2. Which of the following TypeScript code snippets correctly deletes a node at position pos in a singly linked list starting with head?
easy
A. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos - 1; i++) temp = temp.next; temp.next = temp.next.next; }
B. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos; i++) temp = temp.next; temp.next = temp.next.next; }
C. if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i <= pos; i++) temp = temp.next; temp.next = temp.next.next; }
D. if (pos === 0) head = head.next; else { let temp = head; for (let i = 1; i <= pos; i++) temp = temp.next; temp.next = temp.next.next; }

Solution

  1. Step 1: Handle deletion at head (position 0)

    If position is 0, simply move head to next node to delete the first node.
  2. Step 2: Traverse to node before position

    Loop from 0 to pos-1 to reach node before the one to delete, then link its next to next.next.
  3. Final Answer:

    if (pos === 0) head = head.next; else { let temp = head; for (let i = 0; i < pos - 1; i++) temp = temp.next; temp.next = temp.next.next; } -> Option A
  4. Quick Check:

    Loop to pos-1, then skip node [OK]
Hint: Loop to pos-1 to delete node at pos [OK]
Common Mistakes:
  • Looping to pos instead of pos-1
  • Not handling deletion at head correctly
  • Accessing temp.next.next when temp.next is null
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null and the code below, what will be the list after deleting the node at position 2?
let head = { val: 10, next: { val: 20, next: { val: 30, next: { val: 40, next: { val: 50, next: null } } } } };
let pos = 2;
if (pos === 0) head = head.next;
else {
  let temp = head;
  for (let i = 0; i < pos - 1; i++) temp = temp.next;
  temp.next = temp.next.next;
}
medium
A. 10 -> 30 -> 40 -> 50 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> null
C. 10 -> 20 -> 40 -> 50 -> null
D. 20 -> 30 -> 40 -> 50 -> null

Solution

  1. Step 1: Identify node at position 2

    Position 2 node has value 30 (0-based indexing: 10 at 0, 20 at 1, 30 at 2).
  2. Step 2: Delete node at position 2

    Traverse to node at position 1 (value 20), then set its next to node at position 3 (value 40), skipping 30.
  3. Final Answer:

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

    Node 30 removed, neighbors linked [OK]
Hint: Skip node at pos by linking previous to next.next [OK]
Common Mistakes:
  • Deleting wrong node due to off-by-one error
  • Not updating links properly
  • Confusing position indexing
4. The following TypeScript code attempts to delete a node at position pos in a singly linked list but causes a runtime error. What is the error?
function deleteNode(head: Node | null, pos: number): Node | null {
  if (pos === 0) return head.next;
  let temp = head;
  for (let i = 0; i < pos; i++) {
    temp = temp.next!;
  }
  temp.next = temp.next!.next;
  return head;
}
medium
A. Incorrect loop range causes skipping the wrong node
B. Accessing temp.next when temp is null causes runtime error
C. Missing check for empty list before deletion
D. Returning head.next instead of head after deletion

Solution

  1. Step 1: Analyze loop traversal

    The loop runs from 0 to pos, moving temp forward pos times, which can make temp null if pos >= list length.
  2. Step 2: Identify null access

    After loop, temp may be null, so accessing temp.next causes runtime error.
  3. Final Answer:

    Accessing temp.next when temp is null causes runtime error -> Option B
  4. Quick Check:

    Loop overshoots, temp becomes null, then temp.next fails [OK]
Hint: Loop to pos-1, not pos, to avoid null access [OK]
Common Mistakes:
  • Looping too far causing null pointer
  • Not checking for empty list
  • Misunderstanding return value after deletion
5. You have a singly linked list representing a playlist: Song1 -> Song2 -> Song3 -> Song4 -> Song5 -> null. You want to delete the 4th song (position 3). After deletion, you want to insert a new song Song6 at the same position. Which sequence of operations correctly achieves this?
hard
A. Delete node at position 3, then insert new node at head
B. Delete node at position 3, then traverse to position 3 and insert new node after it
C. Delete node at position 2, then traverse to position 3 and insert new node after it
D. Delete node at position 3, then traverse to position 2 and insert new node after it

Solution

  1. Step 1: Delete node at position 3

    Remove the node at position 3 (Song4) by linking node at position 2 to node at position 4.
  2. Step 2: Insert new node at position 3

    Traverse to position 2, then insert new node after it to place Song6 at position 3.
  3. Final Answer:

    Delete node at position 3, then traverse to position 2 and insert new node after it -> Option D
  4. Quick Check:

    Delete at pos 3, insert after pos 2 = new pos 3 [OK]
Hint: Insert after pos-1 to place new node at pos [OK]
Common Mistakes:
  • Inserting at wrong position after deletion
  • Deleting wrong node position
  • Inserting at head instead of correct position
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