Good evening! Here's our prompt for today.
Removing the Nth Node From a Linked List's End: A Single-Pass Approach
Get ready to sail through a classic problem: Removing the n
th 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 n
th node and still maintain the list's structure.

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 n
th 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
.

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 ofO(1)
.
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
'PASSED: Removing the 2nd element from the end should get us `1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 8`'
var assert = require('assert');
​
function removeFromEnd(head, idxFromEnd) {
// remove idxFromEnd-th node
return head;
}
​
function Node(val) {
this.val = val;
this.next = null;
}
​
function LinkedListNode(val) {
this.val = val;
this.next = null;
}
​
var list1 = new LinkedListNode(3);
var nodes1 = [4, 5, 6, 7, 8, 9, 10];
createNodes(list1, nodes1);
​
var list2 = new LinkedListNode(1);
var nodes2 = [2, 3, 4, 5, 6, 7, 8];
createNodes(list2, nodes2);
​
function createNodes(head, nodes) {
for (let i = 0; i < nodes.length; i++) {
var newNode = new LinkedListNode(nodes[i]);
head.next = newNode;
Tired of reading? Watch this video explanation!
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's how we would solve this problem...
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.