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;
}
相关推荐
什巳12 分钟前
JAVA练习306- 翻转二叉树
java·数据结构·算法·leetcode
一个初入编程的小白17 分钟前
C语言:函数栈帧与销毁
c语言·开发语言
smj2302_7968265220 分钟前
解决leetcode第3989题网格中保持一致的最大列数
python·算法·leetcode
巧克力男孩dd2 小时前
Python超典型练习题(第一次作业)
开发语言·python·算法
爱刷碗的苏泓舒2 小时前
平方根信息滤波:矩阵推导及 GNSS 参数估计应用
线性代数·算法·矩阵·gnss·参数估计·测量平差·平方根信息滤波
想做小南娘,发现自己是女生喵3 小时前
第 2 章 顺序表和 vector
java·数据结构·算法
ComputerInBook3 小时前
c 和 c++ 中的宏块(macro)
c语言·c++··宏块·宏指令
艾醒3 小时前
2026年第29周(7.13-7.19)AI全复盘:技术突破、行业趣闻翻车、算力服务器商业动态
人工智能·算法
雪碧聊技术4 小时前
动态规划算法—01背包问题
算法·动态规划
bu_shuo4 小时前
c与cpp中的argc和argv
c语言·c++·算法