Bird
Raised Fist0
DSA Typescriptprogramming

Remove Nth Node from End of List in DSA Typescript

Choose your learning style10 modes available
Mental Model
We want to remove a node counting from the end, so we use two pointers spaced apart to find it in one pass.
Analogy: Imagine two friends walking on a path: one starts ahead by n steps, then both walk together until the first reaches the end. The second friend then points exactly to the node before the one to remove.
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
          ↑         ↑
         fast      slow
Dry Run Walkthrough
Input: list: 1->2->3->4->5, remove 2nd node from end
Goal: Remove the 2nd node from the end, which is node with value 4
Step 1: Move fast pointer 2 steps ahead
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
          slow ↑      fast ↑
Why: To create a gap of 2 nodes between fast and slow pointers
Step 2: Move both pointers one step forward
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
               slow ↑      fast ↑
Why: Keep the gap while moving towards the end
Step 3: Move both pointers one step forward
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
                    slow ↑      fast ↑
Why: Continue moving until fast reaches the last node
Step 4: Move both pointers one step forward; fast reaches null
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
                         slow ↑
Why: Slow is now just before the node to remove
Step 5: Remove node after slow (node 4)
head -> 1 -> 2 -> 3 -> 5 -> null
Why: By skipping node 4, it is removed from the list
Result:
head -> 1 -> 2 -> 3 -> 5 -> null
Annotated Code
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 {
  const dummy = new ListNode(0, head); // dummy node before head
  let fast: ListNode | null = dummy;
  let slow: ListNode | null = dummy;

  // Move fast pointer n steps ahead
  for (let i = 0; i < n; i++) {
    if (fast) fast = fast.next;
  }

  // Move both pointers until fast reaches the end
  while (fast && fast.next) {
    fast = fast.next;
    slow = slow!.next;
  }

  // Remove the nth node from end
  if (slow && slow.next) {
    slow.next = slow.next.next;
  }

  return dummy.next;
}

// Driver code to test
const head = new ListNode(1,
  new ListNode(2,
    new ListNode(3,
      new ListNode(4,
        new ListNode(5)))));

const newHead = removeNthFromEnd(head, 2);

// Function to print list
function printList(node: ListNode | null): void {
  let curr = node;
  let result = '';
  while (curr) {
    result += curr.val + ' -> ';
    curr = curr.next;
  }
  result += 'null';
  console.log(result);
}

printList(newHead);
const dummy = new ListNode(0, head); // dummy node before head
Create dummy node to handle edge cases like removing head
for (let i = 0; i < n; i++) { if (fast) fast = fast.next; }
Advance fast pointer n steps ahead to create gap
while (fast && fast.next) { fast = fast.next; slow = slow!.next; }
Move both pointers until fast reaches the last node
if (slow && slow.next) { slow.next = slow.next.next; }
Skip the target node to remove it from the list
return dummy.next;
Return the new head, which may be different if head was removed
OutputSuccess
1 -> 2 -> 3 -> 5 -> null
Complexity Analysis
Time: O(n) because we traverse the list once with two pointers
Space: O(1) because we use only a few pointers, no extra data structures
vs Alternative: Compared to counting length first then removing, this one-pass method is more efficient and cleaner
Edge Cases
Removing the head node (n equals list length)
Dummy node allows removal without special case code
DSA Typescript
const dummy = new ListNode(0, head); // dummy node before head
List with only one node and n=1
Returns null as list becomes empty
DSA Typescript
if (slow && slow.next) { slow.next = slow.next.next; }
n is 1 (remove last node)
Slow pointer stops at second last node and removes last node
DSA Typescript
while (fast && fast.next) { fast = fast.next; slow = slow!.next; }
When to Use This Pattern
When asked to remove a node from the end of a linked list in one pass, use two pointers spaced n apart to find the target efficiently.
Common Mistakes
Mistake: Not using a dummy node and failing when removing the head node
Fix: Add a dummy node before head to simplify removal logic
Mistake: Moving fast pointer n times but not checking for null, causing errors on short lists
Fix: Check if fast is null before moving to next to avoid runtime errors
Mistake: Moving slow pointer too early or too late, missing the correct node to remove
Fix: Move slow pointer only after fast pointer has moved n steps and while fast.next is not null
Summary
Removes the nth node from the end of a singly linked list in one pass.
Use when you need efficient removal from the end without counting list length first.
The key is to keep two pointers n nodes apart so when the first reaches the end, the second is just before the target.

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