00:00

Problem Statement

Input/Output Examples

Visual Explanation

The dummy node in front of the head means the node before the target always exists, even when the target is the head itself.

Approaches

Both approaches use a dummy head. One keeps two pointers n apart; the other counts the length first.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of nodes (the list length). Both approaches are linear with constant extra memory.

Interview Tips and Recommended Approach

  • Use a dummy node. It removes the special case of deleting the head.
  • Stop on fast.next, not fast, so slow stops one node early.
  • Validate n if the input is not guaranteed to be in range.
  • Reuse the gap idea. Two pointers a fixed distance apart also find the k-th node from the end.

Recommended Approach: Two Pointers, One Pass

Why: it removes the node in a single pass with constant memory, and the dummy node keeps every case, including the head, uniform.