Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Remove Nth Node from End of List in DSA Javascript - 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 of removing 2nd node from end in a 5-node list
What is the printed linked list after removing the 2nd node from the end?
DSA Javascript
class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = 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 list: 1 -> 2 -> 3 -> 4 -> 5 -> null
let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));

head = removeNthFromEnd(head, 2);

// Print list
let output = [];
let current = head;
while (current !== null) {
  output.push(current.val);
  current = current.next;
}
console.log(output.join(' -> ') + ' -> null');
A1 -> 2 -> 3 -> 5 -> null
B1 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> 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 last node from a 3-node list
What is the printed linked list after removing the 1st node from the end (last node) in a 3-node list?
DSA Javascript
class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = 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 list: 10 -> 20 -> 30 -> null
let head = new ListNode(10, new ListNode(20, new ListNode(30)));

head = removeNthFromEnd(head, 1);

// Print list
let output = [];
let current = head;
while (current !== null) {
  output.push(current.val);
  current = current.next;
}
console.log(output.join(' -> ') + ' -> null');
A10 -> 20 -> null
B10 -> 30 -> null
C20 -> 30 -> null
D10 -> 20 -> 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 this Remove Nth Node from End code
What error does this code produce when trying to remove the 5th node from the end in a 4-node list?
DSA Javascript
class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = dummy;
  for (let i = 0; i < n; i++) {
    first = first.next;
  }
  while (first.next !== null) {
    first = first.next;
    second = second.next;
  }
  second.next = second.next.next;
  return dummy.next;
}

// Create list: 5 -> 6 -> 7 -> 8 -> null
let head = new ListNode(5, new ListNode(6, new ListNode(7, new ListNode(8))));

head = removeNthFromEnd(head, 5);
AReturns 6 -> 7 -> 8 -> null
BNo error, returns 5 -> 7 -> 8 -> null
CSyntaxError: Unexpected token
DTypeError: Cannot read property 'next' of null
Attempts:
2 left
💡 Hint
Check the for loop that moves the first pointer n steps ahead.
🧠 Conceptual
advanced
1:30remaining
Why use a dummy node in Remove Nth Node from End?
Why do we use a dummy node before the head in the Remove Nth Node from End algorithm?
ATo make the list circular for easier traversal
BTo handle the case when the head node itself needs to be removed
CTo store the length of the list
DTo avoid using two pointers
Attempts:
2 left
💡 Hint
Think about what happens if the first node is removed.
❓ Predict Output
expert
2:30remaining
Output after removing 4th node from end in a 6-node list
What is the printed linked list after removing the 4th node from the end in this 6-node list?
DSA Javascript
class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = 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 list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))));

head = removeNthFromEnd(head, 4);

// Print list
let output = [];
let current = head;
while (current !== null) {
  output.push(current.val);
  current = current.next;
}
console.log(output.join(' -> ') + ' -> null');
A1 -> 2 -> 3 -> 5 -> 6 -> null
B1 -> 3 -> 4 -> 5 -> 6 -> null
C1 -> 2 -> 4 -> 5 -> 6 -> null
D2 -> 3 -> 4 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint
The 4th node from the end is the node with value 3.

Practice

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

Solution

  1. Step 1: Understand the problem statement

    The problem asks to remove a node counting from the end of a linked list, not from the start.
  2. Step 2: Identify the correct action

    Removing the nth node from the end means deleting that specific node and linking its previous node to the next node.
  3. Final Answer:

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

    Remove nth from end = Delete nth from end [OK]
Hint: Count nodes from end, not start, to remove correct node [OK]
Common Mistakes:
  • Confusing nth from end with nth from start
  • Trying to reverse the list instead of removing
  • Removing the wrong node index
2. Which of the following is the correct way to declare a function in JavaScript to remove the nth node from the end of a singly linked list?
easy
A. function removeNthFromEnd(head, n) { /* code */ }
B. removeNthFromEnd = (head, n) => { /* code */ }
C. All of the above
D. const removeNthFromEnd = function(head, n) { /* code */ }

Solution

  1. Step 1: Review JavaScript function declaration styles

    JavaScript supports function declarations, function expressions, and arrow functions.
  2. Step 2: Check each option

    function removeNthFromEnd(head, n) { /* code */ } is a function declaration, B is an arrow function, C is a function expression. All are valid ways to declare functions.
  3. Final Answer:

    All of the above -> Option C
  4. Quick Check:

    All function styles valid in JS [OK]
Hint: JS allows multiple function declaration styles [OK]
Common Mistakes:
  • Thinking only one function syntax is valid
  • Confusing arrow functions with older syntax
  • Missing const or function keyword
3. What will be the output linked list after running this code snippet?
class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = 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;
}

// 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);

// Print list
let result = [];
while (newHead) {
  result.push(newHead.val);
  newHead = newHead.next;
}
console.log(result);
medium
A. [1, 2, 3, 4]
B. [1, 2, 4, 5]
C. [2, 3, 4, 5]
D. [1, 2, 3, 5]

Solution

  1. Step 1: Understand the code logic

    The code uses two pointers with a dummy node to remove the nth node from the end. Here, n=2 means remove the 2nd last node (value 4).
  2. Step 2: Trace the linked list after removal

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

    [1, 2, 3, 5] -> Option D
  4. Quick Check:

    Remove 2nd last node = remove 4 [OK]
Hint: Use two pointers spaced n apart to find node to remove [OK]
Common Mistakes:
  • Removing the wrong node counting from start
  • Not using dummy node causing errors on head removal
  • Off-by-one error in loop for pointer movement
4. Identify the bug in this code snippet for removing the nth node from the end of a linked list:
function removeNthFromEnd(head, n) {
  let dummy = new ListNode(0, head);
  let first = dummy;
  let second = dummy;
  for (let i = 0; i < n; i++) {
    first = first.next;
  }
  while (first.next !== null) {
    first = first.next;
    second = second.next;
  }
  second.next = second.next.next;
  return dummy.next;
}
medium
A. The while loop condition should be first !== null instead of first.next !== null
B. The for loop should run from i = 0 to i <= n to move first pointer correctly
C. The dummy node is not needed and causes errors
D. second.next = second.next.next should be second.next.next = second.next

Solution

  1. Step 1: Analyze the for loop pointer movement

    The for loop moves the first pointer n times, which is correct to create a gap of n nodes between first and second pointers.
  2. Step 2: Understand the while loop condition

    The while loop condition should be first !== null to ensure the first pointer reaches the end, allowing the second pointer to point to the node before the target.
  3. Final Answer:

    The while loop condition should be first !== null instead of first.next !== null -> Option A
  4. Quick Check:

    Use while (first !== null) to correctly position second pointer [OK]
Hint: Use while (first !== null) to move pointers correctly [OK]
Common Mistakes:
  • Moving first pointer only n times instead of n+1
  • Incorrect while loop condition causing null errors
  • Removing dummy node mistakenly
5. Given a linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, what will be the resulting list after removing the 5th node from the end using the standard two-pointer approach?
hard
A. [1, 2, 3, 4]
B. [2, 3, 4, 5]
C. [1, 3, 4, 5]
D. [1, 2, 3, 5]

Solution

  1. Step 1: Identify the node to remove

    Removing the 5th node from the end means removing the first node (value 1) because the list length is 5.
  2. Step 2: Remove the head node and link next node as new head

    After removal, the list starts from node with value 2: 2 -> 3 -> 4 -> 5.
  3. Final Answer:

    [2, 3, 4, 5] -> Option B
  4. Quick Check:

    Remove 5th from end = remove head [OK]
Hint: Removing nth from end where n = list length removes head [OK]
Common Mistakes:
  • Not handling removal of head node correctly
  • Returning original list without removal
  • Confusing nth from end with nth from start
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