LeetCode 19: Remove Nth Node From End of List

LeetCode 19: Remove Nth Node From End of List

    • [1. Problem Link 🔗](#1. Problem Link 🔗)
    • [2. Solution Overview 🧭](#2. Solution Overview 🧭)
    • [3. Solution 1: Two Pointers with Dummy Node (Recommended)](#3. Solution 1: Two Pointers with Dummy Node (Recommended))
      • [3.1. Algorithm](#3.1. Algorithm)
      • [3.2. Important Points](#3.2. Important Points)
      • [3.3. Java Implementation](#3.3. Java Implementation)
      • [3.4. Time & Space Complexity](#3.4. Time & Space Complexity)
    • [4. Solution 2: Two Pass with Length Calculation](#4. Solution 2: Two Pass with Length Calculation)
      • [4.1. Algorithm思路](#4.1. Algorithm思路)
      • [4.2. Important Points](#4.2. Important Points)
      • [4.3. Java Implementation](#4.3. Java Implementation)
      • [4.4. Time & Space Complexity](#4.4. Time & Space Complexity)
    • [5. Solution 3: Recursive Approach](#5. Solution 3: Recursive Approach)
      • [5.1. Algorithm思路](#5.1. Algorithm思路)
      • [5.2. Important Points](#5.2. Important Points)
      • [5.3. Java Implementation](#5.3. Java Implementation)
      • [5.4. Time & Space Complexity](#5.4. Time & Space Complexity)
    • [6. Solution 4: Stack-based Approach](#6. Solution 4: Stack-based Approach)
      • [6.1. Algorithm思路](#6.1. Algorithm思路)
      • [6.2. Important Points](#6.2. Important Points)
      • [6.3. Java Implementation](#6.3. Java Implementation)
      • [6.4. Time & Space Complexity](#6.4. Time & Space Complexity)
    • [7. Solution 5: Two Pointers without Dummy Node](#7. Solution 5: Two Pointers without Dummy Node)
      • [7.1. Algorithm思路](#7.1. Algorithm思路)
      • [7.2. Important Points](#7.2. Important Points)
      • [7.3. Java Implementation](#7.3. Java Implementation)
      • [7.4. Time & Space Complexity](#7.4. Time & Space Complexity)
    • [8. Solution Comparison 📊](#8. Solution Comparison 📊)
    • [9. Summary 📝](#9. Summary 📝)

LeetCode 19: Remove Nth Node From End of List

2. Solution Overview 🧭

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

Example:

复制代码
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

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

Constraints:

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

Common approaches include:

  • Two Pass with Length Calculation: Calculate length first, then remove (L-n+1)th node
  • One Pass with Two Pointers: Use fast and slow pointers to find the nth node from end
  • Stack-based Approach: Use stack to track nodes (less efficient)

3. Solution 1: Two Pointers with Dummy Node (Recommended)

3.1. Algorithm

  • Use a dummy node to handle edge cases (like removing head)
  • Move fast pointer n steps ahead of slow pointer
  • Move both pointers until fast reaches the end
  • Slow pointer will be at (n+1)th node from end
  • Remove the next node of slow pointer

3.2. Important Points

  • Most efficient and elegant solution
  • Handles edge cases gracefully
  • One pass algorithm

3.3. Java Implementation

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // Create dummy node to handle edge cases
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode slow = dummy;
        ListNode fast = dummy;
        
        // Move fast pointer n steps ahead
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        
        // Move both pointers until fast reaches the end
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        
        // Remove the nth node from end
        slow.next = slow.next.next;
        
        return dummy.next;
    }
}

3.4. Time & Space Complexity

  • Time Complexity: O(L) where L is the length of the list
  • Space Complexity: O(1)

4. Solution 2: Two Pass with Length Calculation

4.1. Algorithm思路

  • First pass: Calculate the length of the list
  • Calculate which node to remove (length - n + 1)
  • Second pass: Traverse to the node before the target and remove it

4.2. Important Points

  • Simple and intuitive
  • Easy to understand and implement
  • Two passes but still efficient

4.3. Java Implementation

java 复制代码
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // Calculate length of the list
        int length = 0;
        ListNode current = head;
        while (current != null) {
            length++;
            current = current.next;
        }
        
        // If removing the head
        if (n == length) {
            return head.next;
        }
        
        // Find the node before the one to remove
        current = head;
        for (int i = 1; i < length - n; i++) {
            current = current.next;
        }
        
        // Remove the node
        current.next = current.next.next;
        
        return head;
    }
}

4.4. Time & Space Complexity

  • Time Complexity: O(L)
  • Space Complexity: O(1)

5. Solution 3: Recursive Approach

5.1. Algorithm思路

  • Use recursion to traverse to the end of the list
  • Use a counter to track the position from the end
  • When counter equals n, skip that node

5.2. Important Points

  • Elegant recursive solution
  • Uses O(L) stack space
  • Good for understanding recursion

5.3. Java Implementation

java 复制代码
class Solution {
    private int count = 0;
    
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // Use a dummy node to handle head removal
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        removeHelper(dummy, n);
        return dummy.next;
    }
    
    private void removeHelper(ListNode node, int n) {
        if (node == null) {
            return;
        }
        
        removeHelper(node.next, n);
        count++;
        
        // When we reach the nth node from end in backtracking
        if (count == n + 1) {
            node.next = node.next.next;
        }
    }
}

5.4. Time & Space Complexity

  • Time Complexity: O(L)
  • Space Complexity: O(L) for recursion stack

6. Solution 4: Stack-based Approach

6.1. Algorithm思路

  • Push all nodes onto a stack
  • Pop n nodes to reach the nth node from end
  • The next node in stack is the one before the target
  • Remove the target node

6.2. Important Points

  • Simple to understand
  • Uses O(L) extra space
  • Good for learning purposes

6.3. Java Implementation

java 复制代码
import java.util.Stack;

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        Stack<ListNode> stack = new Stack<>();
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode current = dummy;
        
        // Push all nodes onto stack
        while (current != null) {
            stack.push(current);
            current = current.next;
        }
        
        // Pop n nodes to find the node to remove
        for (int i = 0; i < n; i++) {
            stack.pop();
        }
        
        // The top of stack is the node before the target
        ListNode nodeBefore = stack.peek();
        nodeBefore.next = nodeBefore.next.next;
        
        return dummy.next;
    }
}

6.4. Time & Space Complexity

  • Time Complexity: O(L)
  • Space Complexity: O(L)

7. Solution 5: Two Pointers without Dummy Node

7.1. Algorithm思路

  • Similar to Solution 1 but without dummy node
  • Handle head removal as a special case
  • More complex edge case handling

7.2. Important Points

  • Saves one node of memory
  • More complex code
  • Good for understanding pointer manipulation

7.3. Java Implementation

java 复制代码
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head;
        ListNode fast = head;
        
        // Move fast pointer n steps ahead
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        
        // If fast is null, we're removing the head
        if (fast == null) {
            return head.next;
        }
        
        // Move both until fast reaches last node
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        
        // Remove the node
        slow.next = slow.next.next;
        
        return head;
    }
}

7.4. Time & Space Complexity

  • Time Complexity: O(L)
  • Space Complexity: O(1)

8. Solution Comparison 📊

Solution Time Complexity Space Complexity Advantages Disadvantages
Two Pointers with Dummy O(L) O(1) Elegant, handles edges well Uses one extra node
Two Pass with Length O(L) O(1) Simple, intuitive Two passes
Recursive O(L) O(L) Elegant, recursive thinking Stack overflow risk
Stack-based O(L) O(L) Easy to understand Extra space usage
Two Pointers without Dummy O(L) O(1) No extra node Complex edge handling

9. Summary 📝

  • Key Insight: The nth node from end is the (L-n+1)th node from beginning
  • Recommended Approach: Solution 1 (Two Pointers with Dummy Node) is most commonly used
  • Critical Technique: Using two pointers with n-step difference to find the target in one pass
  • Pattern Recognition: This is a classic two-pointer pattern for linked list problems

Why Two Pointers Work:

  • When fast pointer is n steps ahead, and both move at same speed
  • When fast reaches the end, slow will be at (L-n)th position
  • This allows us to remove the next node (which is the target)

The two pointers with dummy node approach is generally preferred for its elegance and robustness in handling edge cases.

相关推荐
byzh_rc1 分钟前
[AI编程从入门到入土] 配置文件
java·数据库·ai编程
花千树-0106 分钟前
多步骤 ReAct 实战:让 Agent 自主完成航司比价与订票
java·agent·function call·react agent·harness·j-langchain·多步骤推理
网域小星球8 分钟前
C 语言从 0 入门(十二)|指针与数组:数组名本质、指针遍历数组
c语言·算法·指针·数组·指针遍历数组
Tairitsu_H10 分钟前
C语言:排序(一)
c语言·数据结构·排序
xcLeigh15 分钟前
飞算 JavaAI 进阶实战:从代码生成到系统架构优化的全流程指南
java·系统架构·代码生成·java开发·飞算javaai炫技赛·javaai·飞算
冰糖拌面20 分钟前
二叉树遍历-递归、迭代、Morris
算法
希望永不加班44 分钟前
SpringBoot 事件机制:ApplicationEvent 与监听器
java·开发语言·spring boot·后端·spring
碧海银沙音频科技研究院1 小时前
虚拟机ubuntu与windows共享文件夹(Samba共享)解决WSL加载SI工程满卡问题
人工智能·深度学习·算法
CoovallyAIHub1 小时前
ICLR 2026 | VLM自己学会调检测器:VTool-R1用强化学习教视觉模型使用工具推理
算法·架构·github
CoovallyAIHub1 小时前
RK3588上111 FPS:轻量YOLOv8+异步视频处理系统实现无人机自主电力巡检
算法·架构·github