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;
}
相关推荐
CoderCodingNo6 分钟前
【GESP】C++四级真题 luogu-B4416 [GESP202509 四级] 最长连续段
开发语言·c++·算法
xjxijd6 分钟前
工业元宇宙 IDC 支撑:数字孪生算法 + 边缘服务器,生产调度响应速度提 3 倍
运维·服务器·算法
程序员zgh17 分钟前
代码重构 —— 读后感
运维·c语言·开发语言·c++·重构
xwz小王子23 分钟前
UniBYD:超越人类示教模仿的跨实体机器人操作学习统一框架
学习·算法·机器人·跨实体
跨境卫士苏苏40 分钟前
突围新品广告泥潭:亚马逊广告底层逻辑大重构
大数据·人工智能·算法·重构·亚马逊·防关联
旧梦吟1 小时前
脚本网页 三人四字棋
前端·数据库·算法·css3·html5
凯_kyle1 小时前
Python 算法竞赛 —— 基础篇(更新ing)
笔记·python·算法
lizz311 小时前
C++操作符重载深度解析
java·c++·算法
阿拉斯攀登1 小时前
电子签名:笔迹特征比对核心算法详解
人工智能·算法·机器学习·电子签名·汉王
ytttr8731 小时前
matlab进行利用遗传算法对天线阵列进行优化
开发语言·算法·matlab