Skip to main content

Remove Nth Node From End of List - Solution & Explanation

MediumLinked ListTwo Pointers23 min readAsked at: Amazon, Microsoft, Apple +15
Practice this problem

Problem Statement

Given the head of a linked list, remove the nth node from the end of the list and return its head.

 

Example 1:

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:

Input: head = [1], n = 1
Output: []

Example 3:

Input: head = [1,2], n = 1
Output: [1]

 

Constraints:

  • The number of nodes in the list is sz.
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

 

Follow up: Could you do this in one pass?

Approach Overview

Problem Overview: You are given the head of a singly linked list and an integer n. The task is to remove the nth node from the end of the list and return the updated head. The challenge is handling this in a single traversal while safely updating pointers in a linked list.

Approach 1: Two-Pass Technique (O(n) time, O(1) space)

The simplest strategy is to first determine the length of the linked list. Traverse the list once and count how many nodes it contains. Once you know the total length L, the node to remove is at position L - n from the start. Perform a second traversal to reach the node just before this position, then update its next pointer to skip the target node.

This approach works well because pointer updates are straightforward when you know the exact index to remove. Edge cases require attention, especially when the head itself must be removed (n == L). A common trick is introducing a dummy node before the head so that pointer manipulation remains consistent. Time complexity is O(n) due to two full traversals, and space complexity is O(1) since no additional data structures are required.

Approach 2: One-Pass Technique with Fast and Slow Pointers (O(n) time, O(1) space)

The optimal solution uses the two pointers technique. Maintain two pointers called fast and slow. First move the fast pointer n steps ahead of slow. This creates a fixed gap between them. Then move both pointers forward together until fast reaches the end of the list.

Because the gap remains constant, when fast reaches the last node, slow will be positioned right before the node that must be removed. Updating slow.next = slow.next.next removes the target node in constant time. A dummy head node simplifies cases where the first element must be removed. This approach finishes the task in a single traversal with O(n) time complexity and O(1) auxiliary space.

Recommended for interviews: The fast and slow pointer solution is what most interviewers expect. It demonstrates strong understanding of pointer manipulation and efficient traversal patterns in a linked list. The two-pass method is still valuable to explain first because it shows clear reasoning about list length and index transformation. Then follow with the one-pass approach to demonstrate optimization using the two pointers technique.

Approach 1: Two-Pass Technique

Two-Pass Technique

This approach involves two passes through the linked list to find the node that needs to be removed. In the first pass, compute the length of the list. In the second pass, remove the (Len - n + 1)th node from the start of the list by maintaining a pointer to manage node deletions properly.

This C implementation carries out the Two-Pass Technique. First, it uses a helper function getLength() to compute the length of the list. Then, it iterates to the node immediately before the node to be removed using a dummy node to handle edge cases more efficiently. After removal, it returns the new head of the linked list.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(L), where L is the length of the linked list since we pass over the list twice (to calculate length and to remove the node).
Space Complexity: O(1) since no additional data structures other than few pointers are used.

Try this approach in the editor →

Approach 2: One-Pass Technique with Fast and Slow Pointers

One-Pass Technique with Fast and Slow Pointers

This approach uses two pointers, fast and slow. Begin by moving the fast pointer n steps ahead. Then move both pointers simultaneously until fast reaches the end. This provides the correct position to remove the nth node from the end efficiently in one go.

The C implementation employs two pointers, fast and slow, to move through the list. Initially, the fast pointer is moved n+1 steps for gap creation. As both pointers proceed together, the slow pointer effectively lands at the one preceding the removable node. The code adjusts pointers easily when the fast pointer exhausts the list, ensuring the linked list structure remains valid after node removal.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(L), where L stands for the list's total node count.
Space Complexity: O(1), since a fixed amount of pointers are manipulated.

Try this approach in the editor →

Approach 3: Fast and Slow Pointers

We define two pointers fast and slow, both initially pointing to the dummy head node of the linked list.

Next, the fast pointer moves forward n steps first, then fast and slow pointers move forward together until the fast pointer reaches the end of the linked list. At this point, the node pointed to by slow.next is the predecessor of the n-th node from the end, and we can delete it.

The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Swift

Ruby

C#

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Two-Pass Technique

Time Complexity: O(L), where L is the length of the linked list since we pass over the list twice (to calculate length and to remove the node).
Space Complexity: O(1) since no additional data structures other than few pointers are used.

One-Pass Technique with Fast and Slow Pointers

Time Complexity: O(L), where L stands for the list's total node count.
Space Complexity: O(1), since a fixed amount of pointers are manipulated.

Fast and Slow Pointers

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pass Length CountingO(n)O(1)Best for clarity and when computing list length first simplifies reasoning
One-Pass Fast and Slow PointersO(n)O(1)Preferred interview solution when a single traversal is required

Video Solution

Remove Nth Node from End of List - Oracle Interview Question - Leetcode 19NeetCode292,781 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Remove Nth Node From End of List easy or hard?
Remove Nth Node From End of List is categorized as a Medium difficulty problem. The logic is straightforward once you understand two-pointer traversal, but handling edge cases like removing the head node or lists with a single element can make it tricky.
Remove Nth Node From End of List Python/Java solution
Python and Java implementations typically follow the fast and slow pointer strategy with a dummy head node. The dummy node simplifies edge cases such as removing the first element. Both implementations run in O(n) time and O(1) extra space.
How to solve Remove Nth Node From End of List in O(n)?
Use the two-pointer technique. Advance a fast pointer n steps ahead of a slow pointer, then move both pointers forward until fast reaches the end of the list. At that moment, slow will point to the node before the target. Updating slow.next removes the nth node from the end in O(n) time and O(1) space.
What is the best approach for Remove Nth Node From End of List?
The most efficient approach uses fast and slow pointers. Move the fast pointer n steps ahead, then advance both pointers together until fast reaches the end. The slow pointer will stop right before the node that needs removal. This method completes in O(n) time and O(1) space with only one traversal.
Is Remove Nth Node From End of List asked at Google/Amazon/Meta?
Remove Nth Node From End of List is a common linked list interview problem reported in interviews at companies like Amazon, Google, and Meta. It tests pointer manipulation, edge case handling, and understanding of the two-pointer technique in singly linked lists.
What data structure is used in Remove Nth Node From End of List?
The problem operates on a singly linked list. The solution relies on pointer manipulation to traverse nodes and update next references. The optimal algorithm also applies the two-pointer technique to maintain a fixed gap between nodes during traversal.
What is the time complexity of Remove Nth Node From End of List?
Both common solutions run in O(n) time where n is the number of nodes in the linked list. The two-pass approach traverses the list twice to compute length and remove the node. The optimized fast–slow pointer technique completes the task in a single traversal with O(1) extra space.

Ready to solve this problem?

Practice Remove Nth Node From End of List with our built-in code editor and test cases.

Practice on FleetCode