AlgoDaily

Delete Node From End

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Description

Removing the Nth Node From a Linked List's End: A Single-Pass Approach

Get ready to sail through a classic problem: Removing the nth node from the end of a linked list in a single pass. By the end of this lesson, you'll be able to wave goodbye to that pesky nth node and still maintain the list's structure.

Linked List Visual

The Task

Imagine a chain of dominoes, each representing a node in our linked list. In this scenario, a list might look like this:

1 -> 2 -> 3 -> 4 -> 5

Your mission is to remove the nth domino from the end. So, if we call removeFromEnd(head, 2), the chain would lose the 4 domino, and the head would still be 1.

Linked List After Removal

Ground Rules

Before you jump in, let's set some ground rules:

  • Empty List Handling: The linked list could be empty, akin to a chain with no dominoes.
  • Nth Node Edge Case: If n exceeds the length of the list, then the chain remains intact. No dominoes are toppled!
  • Efficiency Requirements: Aim for a time complexity of O(n) and a space complexity of O(1).
```js
function removeFromEnd(head, idxFromEnd) {
  // remove idxFromEnd-th node
  return head;
};
```

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →