LeetCode //C - 2095. Delete the Middle Node of a Linked List

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 2095. Delete the Middle Node of a Linked List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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]th 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 are 0, 1, 1, 2, and 2, respectively.
 

Example 1:

LeetCode //C - 2095. Delete the Middle Node of a Linked List,LeetCode,leetcode,c语言,算法

Input head = [1,3,4,7,1,2,6]
Output [1,3,4,1,2,6]
Explanation
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
We return the new list after removing this node.

Example 2:

LeetCode //C - 2095. Delete the Middle Node of a Linked List,LeetCode,leetcode,c语言,算法

Input head = [1,2,3,4]
Output [1,2,4]
Explanation
The above figure represents the given linked list.
For n = 4, node 2 with value 3 is the middle node, which is marked in red.

Example 3:

LeetCode //C - 2095. Delete the Middle Node of a Linked List,LeetCode,leetcode,c语言,算法

Input head = [2,1]
Output [2]
Explanation
The above figure represents the given linked list.
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.

Constraints:
  • The number of nodes in the list is in the range [ 1 , 1 0 5 ] [1, 10^5] [1,105].
  • 1 < = N o d e . v a l < = 1 0 5 1 <= Node.val <= 10^5 1<=Node.val<=105

From: LeetCode
Link: 2095. Delete the Middle Node of a Linked List


Solution:

Ideas:

This code uses the fast and slow pointer technique to find the middle node. The fast pointer moves two steps at a time, while the slow pointer moves one step at a time. When the fast pointer reaches the end of the list, the slow pointer will be at the middle node. We then delete the middle node and return the modified list.文章来源地址https://www.toymoban.com/news/detail-783691.html

Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* deleteMiddle(struct ListNode* head) {
    if (head == NULL || head->next == NULL) {
        free(head);
        return NULL;
    }
    
    struct ListNode *slow = head;
    struct ListNode *fast = head;
    struct ListNode *prev = NULL;
    
    // Use the fast and slow pointer approach to find the middle node
    while (fast != NULL && fast->next != NULL) {
        fast = fast->next->next;
        prev = slow;
        slow = slow->next;
    }
    
    // Delete the middle node
    prev->next = slow->next;
    free(slow);
    
    return head;
}

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

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

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

相关文章

  • leetcode - 2616. Minimize the Maximum Difference of Pairs

    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents th

    2024年02月13日
    浏览(31)
  • Lost in the Middle: How Language Models Use Long Contexts

    本文是LLM系列文章,针对《Lost in the Middle: How Language Models Use Long Contexts》的翻译。 虽然最近的语言模型能够将长上下文作为输入,但人们对它们使用长上下文的情况知之甚少。我们分析了语言模型在两项任务中的性能,这两项任务需要在输入上下文中识别相关信息:多文档问

    2024年02月09日
    浏览(33)
  • USACO12OPEN Balanced Cow Subsets G(meet in the middle)

    洛谷P3067 [USACO12OPEN] Balanced Cow Subsets G 我们定义一个奶牛集合 S S S 是平衡的,当且仅当满足以下两个条件: S S S 非空 S S S 可以被划分为两个集合 A , B A,B A , B ,满足 A A A 里的奶牛产量之和等于 B B B 里的牛奶产量之和 现在给定大小为 n n n 的奶牛集合 S S S ,询问它有多少个子

    2024年02月08日
    浏览(28)
  • Failed to execute ‘removeChild‘ on ‘Node‘: The node to be removed is not a child of this node.

    在React项目开发的时候遇到了这种报错,曾经百思不得其解。之前一个表格的时候都是好好的, 但是这次用了tabs切换两个表格之后就出现了这个问题... 发现问题的操作就是:页面刷新之后直接点击tabs默认显示的表格中的某个单元格就直接报错了。 可能是如下报错信息,这个

    2024年02月12日
    浏览(34)
  • LeetCode //C - 19. Remove Nth Node From End of List

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

    2024年02月10日
    浏览(28)
  • 论文解读: 2023-Lost in the Middle: How Language Models Use Long Contexts

    大模型使用的关键在于Prompt,然而大模型存在幻觉现象,如何减少这种现象的发生成为迫切解决的问题。外部知识库+LLM的方法可以缓解大模型幻觉,但是如何撰写Prompt才能发挥LLM的性能。下面介绍这篇论文说明上下文信息出现在Prompt什么位置使模型表现最佳,以及上下文文本

    2024年02月17日
    浏览(35)
  • 【每日一题】Leetcode - 19. Remove Nth Node From End of List

    Leetcode - 19. Remove Nth Node From End of List Drawing on the method of finding midpoints in linked lists, use quick slow pointer Finding midpoints in linked lists nothing

    2024年02月12日
    浏览(40)
  • 【node.js】关于node.js,如何解决npm should be run outside of the Node.js REPL, in your normal shell报错?

    前言,安装node方式采用的是安装包解压的 报如下错误: npm should be run outside of the Node.js REPL, in your normal shell 在windows环境下安装完node,其目录结构下会有一个node.exe文件,运行该工具就相当于在cmd中执行node命令,即进入node开发模式。 产生这个错误的原因是你将npm命令运行在

    2024年02月03日
    浏览(39)
  • npm v10.0.0 is known not to run on Node.js v12.8.0. This version of npm supports the following node

    执行命令时报错:ERROR: npm vxxx is known not to run on Node.js vxxx.  This version of npm supports the following node versions: `^18.17.0 || =20.5.0`. You can find the latest version at https://nodejs.org/. 报错说你现在的npm版本是vxxx 不支持你现在Node的版本vxxx,需要把node版本改成npm支持的版本,在node官网你可以找

    2024年02月08日
    浏览(38)
  • Remove the specified nodes in the linked list with dummy header

    分数 20 作者 伍建全 单位 重庆科技大学 Please create a function with the prototype void removeNode(List L, int key) . This function deletes all nodes from the linked list L where the data field is equal to key .If there are no nodes in the list where the data field is equal to key , the function should do nothing. Structure description: The nod

    2024年04月22日
    浏览(33)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包