Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
HomeLearnInterview Coding PatternsLinked List Patterns
MediumLinked Lists

Remove Nth Node From End of List

linked-listtwo-pointers

Problem

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

Examples

Example 1

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

Output: [1,2,3,5]

Explanation: The 2nd node from the end (value 4) is removed.

Example 2

Input: head = [1], n = 1

Output: []

Explanation: Removing the only node leaves an empty list.

Constraints

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

Hints

Hint 1

You don't know the list's length in advance without a first pass to count it — or do you? Two pointers, offset by n, can find the answer in a single pass.

Hint 2

If one pointer starts n steps ahead of the other, and both advance together, the trailing pointer reaches the target position exactly when the leading pointer reaches the end.

Hint 3

A dummy node before head avoids a special case when the node to remove is the head itself (i.e. n == sz).

Solutions

public ListNode removeNthFromEndTwoPass(ListNode head, int n) {
    int length = 0;
    for (ListNode curr = head; curr != null; curr = curr.next) length++;

    ListNode dummy = new ListNode(0, head);
    ListNode curr = dummy;
    for (int i = 0; i < length - n; i++) curr = curr.next; // walk to the node just before the target
    curr.next = curr.next.next;
    return dummy.next;
}

Time: O(n) · Space: O(1)

Previous · Practice problem

Linked List Cycle II

Next · Practice problem

Valid Parentheses