LeetCode //C - 19. Remove Nth Node From End of List

19. Remove Nth Node From End of List

Given the head of a linked list, remove the n t h n^{th} 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

From: LeetCode

Link: 19. Remove Nth Node From End of List


Solution:

Ideas:

Key Concepts:

  1. Two-Pointer Technique: We use two pointers that initially point to the head of the list.
  2. Dummy Node: A dummy node is used to handle edge cases more easily, like when the head node itself needs to be removed.

Algorithm:

  1. Create a Dummy Node: A dummy node is created and set to point to the head of the list. This simplifies the code for edge cases.

  2. Initialize Two Pointers: Both first and second pointers are initialized to point to the dummy node.

  3. Advance the First Pointer: The first pointer is advanced n+1 steps from the beginning. This creates a gap of n nodes between first and second.

  4. Move Both Pointers: Both first and second pointers are moved one step at a time until first reaches the end of the list. The gap of n nodes is maintained between first and second.

  5. Remove Node: At this point, second will be pointing to the node immediately before the node that needs to be removed. We remove the n-th node from the end.

  6. Return New Head: The new head of the list is returned after freeing the dummy node.

Code:
c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
  struct ListNode *dummy = malloc(sizeof(struct ListNode));
  dummy->val = 0;
  dummy->next = head;
  
  struct ListNode *first = dummy;
  struct ListNode *second = dummy;
  
  // Advance first pointer by n+1 steps from the beginning,
  // so the gap between first and second is n nodes apart
  for (int i = 1; i <= n + 1; i++) {
    first = first->next;
  }
  
  // Move first to the end, maintaining the gap
  while (first != NULL) {
    first = first->next;
    second = second->next;
  }
  
  // Remove the n-th node from the end
  struct ListNode *temp = second->next;
  second->next = second->next->next;
  free(temp);
  
  // Return new head node
  struct ListNode *newHead = dummy->next;
  free(dummy);
  
  return newHead;
}
相关推荐
dongf2019几秒前
R语言KNN算法
算法·数据分析·r语言
小O的算法实验室21 分钟前
2025年IEEE TASE,基于双层耦合平均场博弈的大规模智能体集成任务分配与轨迹规划
人工智能·算法·机器学习
8Qi827 分钟前
LeetCode 337:打家劫舍 III(House Robber III)—— 题解 ✅
算法·leetcode·二叉树·动态规划
地平线开发者27 分钟前
从 INT64 Div 算子约束到 Cast 修复全流程
算法
2601_9611940227 分钟前
教资科三美术考什么|初中高中美术题型考点和模板资料
leetcode·elasticsearch·职场和发展·蓝桥杯·pat考试·lucene
AI科技星30 分钟前
基于奇合数边界的离散解析数论与双螺旋宇宙本体大统一体系论文全部数学公式汇总表
人工智能·算法·机器学习·架构·学习方法
SoftLipaRZC40 分钟前
C语言预处理详解:从宏定义到条件编译
c语言·开发语言
地平线开发者41 分钟前
Horizon 模型多 Batch 配置
算法·自动驾驶
czhaii1 小时前
GB2312简体中文编码表
单片机·算法
8Qi81 小时前
LeetCode 121 & 122:股票买卖问题(DP 对比题解)✅
算法·leetcode·职场和发展·动态规划