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

这篇具有很好参考价值的文章主要介绍了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:

LeetCode //C - 19. Remove Nth Node From End of List,LeetCode,leetcode,c语言,算法

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.文章来源地址https://www.toymoban.com/news/detail-688900.html

Code:
/**
 * 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;
}

到了这里,关于LeetCode //C - 19. Remove Nth Node From End of List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 82. Remove Duplicates from Sorted List II

    Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return the linked list sorted as well.   Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of nodes in the list is in the range [0, 300]

    2024年02月10日
    浏览(33)
  • LeetCode //C - 2095. Delete the Middle Node of a Linked List

    You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the [ n / 2 ] t h [n / 2]^{th} [ n /2 ] t h node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x. For n = 1, 2, 3, 4, and 5, the middle nodes a

    2024年02月02日
    浏览(32)
  • LeetCode //80. Remove Duplicates from Sorted Array II

    Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums

    2024年02月13日
    浏览(37)
  • 【论文笔记】An End-to-End Framework of Road User Detection, Tracking, and Prediction from Monocular Images

    原文链接:https://arxiv.org/abs/2308.05026 目前的轨迹预测方法多基于道路使用者的真实信息,但在实际自动驾驶系统中这些信息是通过检测和跟踪模块得到的,不可避免的存在噪声。本文将感知模块与轨迹预测整合,进行端到端的检测、跟踪和轨迹预测。 本文感知模块使用单目图

    2024年04月28日
    浏览(28)
  • 客户端Unexpected end of file from server 和 服务端nginx 408 的一种解决方法

    https://www.jiweichengzhu.com/article/81044c11caf54753aeed94ef8fad1070 https://blog.csdn.net/chenz_yang/article/details/77238532 终端设备(Debian系统、戴尔服务器)通过HTTPS向云端上报数据时,有的数据如设备心跳能上报,有的数据如服务器配置信息上报时报错: Unexpected end of file from server “Unexpected en

    2024年02月15日
    浏览(50)
  • LeetCode //2675. Array of Objects to Matrix (Day 19 of LC JavaScript Challenge)

    Write a function that converts an array of objects arr into a matrix m. arr is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values. The first row m should be the column names. If there is no nesting, the column names are the unique keys

    2024年02月06日
    浏览(33)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(37)
  • LeetCode 1019. Next Greater Node In Linked List【单调栈模板】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月18日
    浏览(25)
  • Arrays.asList() 返回的list不能add,remove

    Arrays.asList()返回的是List,而且是一个定长的List,所以不能转换为ArrayList,只能转换为AbstractList 原因在于asList()方法返回的是某个数组的列表形式,返回的列表只是数组的另一个视图,而数组本身并没有消失,对列表的任何操作最终都反映在数组上. 所以不支持remove,add方法的 下面是

    2024年02月13日
    浏览(45)
  • 【CSS3】CSS3 结构伪类选择器 ( E:first-child / E:last-child 选择器 | E:nth-child(n) 选择器 | E:nth-of-type 选择器 )

    常见的 结构伪类选择器 : E:first-child 选择器 : E 表示 HTML 标签类型 , 该选择器 选择 匹配的父容器 中的 第一个 E 类型标签 子元素 ; E:last-child 选择器 : 该选择器 选择 匹配的父容器 中的 最后一个 E 类型标签 子元素 ; E:nth-child(n) 选择器 : 该选择器 选择 匹配的父容器 中的 第

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包