Bird
Raised Fist0
DSA Typescriptprogramming~30 mins

Remove Nth Node from End of List in DSA Typescript - Build from Scratch

Choose your learning style10 modes available
Remove Nth Node from End of List
📖 Scenario: Imagine you have a list of tasks to do, and you want to remove the task that is nth from the end of the list. This helps you manage your tasks by removing the one that is not too recent but also not the oldest.
🎯 Goal: You will build a program that removes the nth node from the end of a singly linked list and then prints the updated list.
📋 What You'll Learn
Create a singly linked list with exact nodes: 1 -> 2 -> 3 -> 4 -> 5 -> null
Create a variable n with the value 2
Write a function removeNthFromEnd that removes the nth node from the end
Print the linked list after removal in the format: 1 -> 2 -> 3 -> 5 -> null
💡 Why This Matters
🌍 Real World
Removing the nth node from the end of a list is useful in task management apps, undo features, or any system where recent items need to be removed efficiently.
💼 Career
Understanding linked list manipulation and two-pointer techniques is important for software engineering interviews and working with low-level data structures.
Progress0 / 4 steps
1
Create the linked list
Create a singly linked list with nodes having values 1, 2, 3, 4, and 5 in order. Use the class ListNode provided below and create a variable called head that points to the first node.
DSA Typescript
Hint

Start by creating the last node with value 5 and null next, then wrap it inside nodes with values 4, 3, 2, and 1.

2
Set the variable n
Create a variable called n and set it to the number 2. This means we want to remove the 2nd node from the end of the list.
DSA Typescript
Hint

Just write const n = 2; below the linked list.

3
Write the function to remove the nth node from end
Write a function called removeNthFromEnd that takes head of type ListNode | null and n of type number. It should remove the nth node from the end of the list and return the new head. Use two pointers technique: move the first pointer n steps ahead, then move both pointers until the first reaches the end. Remove the target node by changing links.
DSA Typescript
Hint

Use a dummy node before head to handle edge cases. Move first pointer n+1 steps, then move both pointers until first is null. Change second.next to skip the target node.

4
Print the updated linked list
Call the function removeNthFromEnd with head and n. Store the result in a variable called newHead. Then print the linked list starting from newHead in the format: 1 -> 2 -> 3 -> 5 -> null. Use a loop to traverse and print nodes.
DSA Typescript
Hint

Call removeNthFromEnd(head, n) and save the result. Then loop through the list and build a string with each node's value followed by ' -> '. End with 'null'. Print the string.

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