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;
}
相关推荐
NAGNIP2 小时前
一文搞懂机器学习中的特征降维!
算法·面试
NAGNIP2 小时前
一文搞懂机器学习中的特征构造!
算法·面试
Learn Beyond Limits2 小时前
解构语义:从词向量到神经分类|Decoding Semantics: Word Vectors and Neural Classification
人工智能·算法·机器学习·ai·分类·数据挖掘·nlp
你怎么知道我是队长3 小时前
C语言---typedef
c语言·c++·算法
带土13 小时前
5. enum(枚举)关键字在C/C++中的作用
c语言·c++
Qhumaing4 小时前
C++学习:【PTA】数据结构 7-1 实验7-1(最小生成树-Prim算法)
c++·学习·算法
踩坑记录6 小时前
leetcode hot100 3.无重复字符的最长子串 medium 滑动窗口(双指针)
python·leetcode
Z1Jxxx6 小时前
01序列01序列
开发语言·c++·算法
汽车仪器仪表相关领域7 小时前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试