SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Remove Nth Node From End of List

Use a fixed pointer gap to remove a linked-list node in one pass.

Try It Yourself

Remove the nth node counted from the end. When n = 1, remove the tail. Return the head of the remaining list.

Find the node without measuring the list first

The removed node may be the head, the tail, or the only node.

Submit your implementation when it passes the examples.

Remove Nth Node From End of List

mediumLinked List · Two Pointers30 minLC 19
Problem

Given the head of a singly linked list and an integer n, remove the nth node counted from the end.

Return the list's head after the removal. Each node has the shape { val, next }.

Example 1:

Input: head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]
Explanation: Counting from the end, 5 is first and 4 is second. Removing 4 leaves [1, 2, 3, 5].

Example 2:

Input: head = [1], n = 1
Output: []
Explanation: The only node is first from the end. Removing it leaves an empty list.

Constraints

  • 1 ≤ n ≤ number of nodes ≤ 30
  • The list contains at least one node.
0 attempts

Test results

Results appear here after a run.

Solution

With fast just past node 60 and slow at 40, slow.next is 50, the second node from the end.

Start slow at a dummy node just before the head and fast at the head. Advance fast by n links, then move both pointers together until fast reaches null. Throughout the paired walk, fast is n links ahead of slow.next.

In the diagram, n = 2. From node 50 it takes two links to reach null, through 60. When fast reaches null, slow points to 40, so link 40 directly to 60. If n equals the entire list length, slow stays at the dummy and the same update removes the head.

function removeNthFromEnd(head, n) {
  const dummy = { val: 0, next: head };
  let fast = head;
  let slow = dummy;

  for (let step = 0; step < n; step++) {
    fast = fast.next;
  }

  while (fast !== null) {
    fast = fast.next;
    slow = slow.next;
  }

  slow.next = slow.next.next;
  return dummy.next;
}

Big O notation

MeasureComplexityExplanation
TimeO(L)For a list of length L, each pointer advances at most L times. The input n specifies the position counted from the end.
Auxiliary spaceO(1)The algorithm uses one dummy node and two pointers.

How is this practice?

Previous lessonMerge Two Sorted ListsNext lessonStack
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation