Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Remove Nth Node from End of List in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after removing 2nd node from end
What is the printed linked list after removing the 2nd node from the end in the given list?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
  let dummy = new ListNode(0, head);
  let first: ListNode | null = dummy;
  let second: ListNode | null = dummy;

  for (let i = 0; i <= n; i++) {
    if (first) first = first.next;
  }

  while (first !== null) {
    first = first.next;
    if (second) second = second.next;
  }

  if (second && second.next) {
    second.next = second.next.next;
  }

  return dummy.next;
}

// Create linked list: 1 -> 2 -> 3 -> 4 -> 5 -> null
let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));

let newHead = removeNthFromEnd(head, 2);

// Function to print list
function printList(node: ListNode | null): string {
  let result = [];
  while (node !== null) {
    result.push(node.val);
    node = node.next;
  }
  return result.join(' -> ') + ' -> null';
}

console.log(printList(newHead));
A1 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 4 -> 5 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
❓ Predict Output
intermediate
2:00remaining
Output after removing 1st node from end
What is the printed linked list after removing the last node (1st from end) from the list?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
  let dummy = new ListNode(0, head);
  let first: ListNode | null = dummy;
  let second: ListNode | null = dummy;

  for (let i = 0; i <= n; i++) {
    if (first) first = first.next;
  }

  while (first !== null) {
    first = first.next;
    if (second) second = second.next;
  }

  if (second && second.next) {
    second.next = second.next.next;
  }

  return dummy.next;
}

// Create linked list: 10 -> 20 -> 30 -> null
let head = new ListNode(10, new ListNode(20, new ListNode(30)));

let newHead = removeNthFromEnd(head, 1);

function printList(node: ListNode | null): string {
  let result = [];
  while (node !== null) {
    result.push(node.val);
    node = node.next;
  }
  return result.join(' -> ') + ' -> null';
}

console.log(printList(newHead));
A10 -> 20 -> null
B10 -> 30 -> null
C10 -> 20 -> 30 -> null
D20 -> 30 -> null
Attempts:
2 left
💡 Hint
Removing the last node means the node with value 30 is removed.
🔧 Debug
advanced
2:00remaining
Identify the error in the removeNthFromEnd implementation
What error will this code produce when removing the 3rd node from the end in a 3-node list?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
  let dummy = new ListNode(0, head);
  let first: ListNode | null = dummy;
  let second: ListNode | null = dummy;

  for (let i = 0; i <= n; i++) {
    first = first!.next!;
  }

  while (first !== null) {
    first = first.next;
    second = second!.next;
  }

  second!.next = second!.next!.next;

  return dummy.next;
}

// Create linked list: 1 -> 2 -> 3 -> null
let head = new ListNode(1, new ListNode(2, new ListNode(3)));

let newHead = removeNthFromEnd(head, 3);

function printList(node: ListNode | null): string {
  let result = [];
  while (node !== null) {
    result.push(node.val);
    node = node.next;
  }
  return result.join(' -> ') + ' -> null';
}

console.log(printList(newHead));
ATypeError: Cannot read property 'next' of null
B1 -> 2 -> null
C2 -> 3 -> null
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Check the for loop and how many times first pointer moves.
❓ Predict Output
advanced
2:00remaining
Result after removing 4th node from end in 5-node list
What is the printed linked list after removing the 4th node from the end in the list 10 -> 20 -> 30 -> 40 -> 50 -> null?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
  let dummy = new ListNode(0, head);
  let first: ListNode | null = dummy;
  let second: ListNode | null = dummy;

  for (let i = 0; i <= n; i++) {
    first = first!.next;
  }

  while (first !== null) {
    first = first.next;
    second = second!.next;
  }

  second!.next = second!.next!.next;

  return dummy.next;
}

// Create linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null
let head = new ListNode(10, new ListNode(20, new ListNode(30, new ListNode(40, new ListNode(50)))));

let newHead = removeNthFromEnd(head, 4);

function printList(node: ListNode | null): string {
  let result = [];
  while (node !== null) {
    result.push(node.val);
    node = node.next;
  }
  return result.join(' -> ') + ' -> null';
}

console.log(printList(newHead));
A20 -> 30 -> 40 -> 50 -> null
B10 -> 30 -> 40 -> 50 -> null
C10 -> 20 -> 40 -> 50 -> null
D10 -> 20 -> 30 -> 50 -> null
Attempts:
2 left
💡 Hint
The 4th node from the end is the 2nd node from the start.
🧠 Conceptual
expert
2:00remaining
Why use a dummy node in Remove Nth Node from End?
Why is a dummy node often used at the start of the linked list in the Remove Nth Node from End algorithm?
ATo store the length of the list for quick access during removal.
BTo increase the speed of the algorithm by reducing pointer moves.
CTo simplify edge cases like removing the head node by providing a fixed previous node.
DTo avoid using recursion and prevent stack overflow.
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.

Practice

(1/5)
1. What does the problem Remove Nth Node from End of List ask you to do?
easy
A. Find the value of the nth node from the start
B. Add a new node at the nth position from the start
C. Reverse the linked list up to the nth node
D. Delete the node that is the nth from the end in a linked list

Solution

  1. Step 1: Understand the problem statement

    The problem requires removing the node that is nth from the end of a singly linked list.
  2. Step 2: Clarify the task

    We do not add or reverse nodes; we specifically remove the nth node counting from the list's end.
  3. Final Answer:

    Delete the node that is the nth from the end in a linked list -> Option D
  4. Quick Check:

    Remove nth from end = Delete nth from end [OK]
Hint: Focus on 'from end' meaning count backwards [OK]
Common Mistakes:
  • Confusing nth from end with nth from start
  • Thinking the problem is about reversing the list
  • Assuming insertion instead of deletion
2. Which of the following TypeScript function signatures correctly represents a function to remove the nth node from the end of a singly linked list?
easy
A. function removeNthFromEnd(head: ListNode, n: string): ListNode
B. function removeNthFromEnd(head: number[], n: number): number[]
C. function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null
D. function removeNthFromEnd(n: number, head: ListNode): ListNode

Solution

  1. Step 1: Check parameter types and order

    The function should take the head of a linked list (ListNode or null) and a number n, returning the possibly updated head.
  2. Step 2: Validate correct types and order

    function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null matches this: head is ListNode|null, n is number, return type is ListNode|null.
  3. Final Answer:

    function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null -> Option C
  4. Quick Check:

    Correct signature uses ListNode and number types [OK]
Hint: Head first, then n; use ListNode type [OK]
Common Mistakes:
  • Swapping parameter order
  • Using array type instead of ListNode
  • Using wrong type for n (string instead of number)
3. What is the output linked list after removing the 2nd node from the end in the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
medium
A. 1 -> 2 -> 4 -> 5 -> null
B. 1 -> 2 -> 3 -> 5 -> null
C. 1 -> 3 -> 4 -> 5 -> null
D. 2 -> 3 -> 4 -> 5 -> null

Solution

  1. Step 1: Identify the 2nd node from the end

    Counting from the end: 5 is 1st, 4 is 2nd node from the end.
  2. Step 2: Remove the 4th node

    Removing node with value 4 results in list: 1 -> 2 -> 3 -> 5 -> null.
  3. Final Answer:

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

    2nd from end is 4, removed list matches 1 -> 2 -> 3 -> 5 -> null [OK]
Hint: Count from end, remove that node [OK]
Common Mistakes:
  • Removing wrong node counting from start
  • Removing node 3 instead of 4
  • Confusing 2nd from end with 2nd from start
4. Given this TypeScript code snippet to remove the nth node from end, what is the bug?
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
  let fast = head;
  let slow = head;
  for (let i = 0; i < n; i++) {
    fast = fast.next;
  }
  while (fast.next !== null) {
    fast = fast.next;
    slow = slow.next;
  }
  slow.next = slow.next.next;
  return head;
}
medium
A. It does not handle the case when n equals the length of the list
B. The loop runs one time too many causing runtime error
C. The slow pointer moves too fast and skips nodes
D. It returns the wrong head node

Solution

  1. Step 1: Analyze fast pointer movement

    Fast pointer moves n steps ahead. If n equals list length, fast becomes null.
  2. Step 2: Check while loop condition

    Code uses fast.next, but if fast is null, accessing fast.next causes error or skips removing head.
  3. Step 3: Identify missing edge case handling

    Code does not handle removing the first node when n equals list length.
  4. Final Answer:

    It does not handle the case when n equals the length of the list -> Option A
  5. Quick Check:

    Edge case n = list length missing [OK]
Hint: Check if fast pointer is null after initial moves [OK]
Common Mistakes:
  • Ignoring edge case when removing head node
  • Assuming fast.next always exists
  • Not returning updated head when head is removed
5. You have a linked list 1 -> 2 -> 3 -> 4 -> 5 -> null. You remove the 5th node from the end twice in a row. What is the final linked list?
hard
A. 2 -> 3 -> 4 -> 5 -> null
B. 3 -> 4 -> 5 -> null
C. 3 -> 4 -> null
D. 4 -> 5 -> null

Solution

  1. Step 1: First removal of 5th node from end

    List length 5, 5th from end is head (1). After removal: 2 -> 3 -> 4 -> 5 -> null.
  2. Step 2: Second removal of 5th node from end

    New length 4, n=5 > 4 so no 5th node exists; list unchanged: 2 -> 3 -> 4 -> 5 -> null.
  3. Final Answer:

    2 -> 3 -> 4 -> 5 -> null -> Option A
  4. Quick Check:

    Removing 5th from end twice removes head once [OK]
Hint: Removing nth from end twice may remove head once only [OK]
Common Mistakes:
  • Confusing nth from end with nth from start
  • Thinking the problem is about reversing the list
  • Assuming insertion instead of 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