leetcode - 2095. Delete the Middle Node of a Linked List

news2024/9/22 9:49:18

Description

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⌋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:
在这里插入图片描述

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:
在这里插入图片描述

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:
在这里插入图片描述

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, 10^5].
1 <= Node.val <= 10^5

Solution

Use fast, slow pointer. Every time, fast pointer moves 2 steps and slow pointer moves 1 step. When fast pointer reaches the end, the slow pointer points to the middle.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        ret_head = ListNode(-1)
        ret_head.next = head
        slow, fast = ret_head, head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        # delete slow.next
        slow.next = slow.next.next
        return ret_head.next

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1487848.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【小白友好】LeetCode 打家劫舍 III

https://leetcode.cn/problems/house-robber-iii/description/ 前言 建议还是先看看动态规划的基础题再看这个。动态规划是不刷题&#xff0c;自己100%想不出来的。 基础题&#xff1a; 最大子数组和乘积最大子数组最长递增子序列 最大升序子数组和 小白想法 现在我们想遍…

使用query请求数据出现500的报错

我在写项目的时候遇到了一个问题&#xff0c;就是在存商品id的时候我将它使用了JSON.stringify的格式转换了&#xff01;&#xff01;&#xff01;于是便爆出了500这个错误&#xff01;&#xff01;&#xff01; 我将JSON.stringify的格式去除之后&#xff0c;它就正常显示了&…

Linux - 进程控制

1、进程创建 1.1、fork函数初识 在linux中fork函数时非常重要的函数&#xff0c;它从已存在进程中创建一个新进程。新进程为子进程&#xff0c;而原进程为父进程&#xff1b; #include <unistd.h> pid_t fork(void); 返回值&#xff1a;自进程中返回0&#xff0c;父进…

java常见的8种数据结构

一、线性结构&#xff1a;数组、链表、哈希表&#xff1b;队列、栈 1.数组&#xff1a; 数组是有序元素的序列&#xff0c;在内存中的分配是连续的&#xff0c;数组会为存储的元素都分配一个下标&#xff08;索引&#xff09;&#xff0c;此下标是一个自增连续的&#xff0c;访…

万村乐数字乡村系统开源代码:革命性引领,助推乡村振兴新篇章

如今&#xff0c;国际社会普遍认为信息化、数字化已是重大且不可逆转的发展趋势&#xff0c;如何让广大农村地区充分分享到这个发展带来的红利&#xff0c;从而提升农村的经济活力&#xff0c;确保村民生活质量不断优化&#xff0c;已然成为我们需要认真研究并积极解决的重大议…

美国法院命令NSO集团将其间谍软件代码交给WhatsApp

Techreport网站消息&#xff0c;近日&#xff0c;美国法院下令要求以色列间谍软件开发商NSO集团将其Pegasus间谍软件的代码交给WhatsApp。 2019年&#xff0c;NSO集团利用WhatsApp的安全漏洞对1400名用户进行了为期两周的监视。同年&#xff0c;WhatsApp向该公司提起了法律诉讼…

k8s初始化错误

报错详情&#xff1a; you can check the kubelet logs for further clues by running: ‘journalctl -u kubelet’ Alternatively, there might be issues with your Kubernetes configuration files or maybe the necessary ports are not opened. Check the status of …

应用方案丨D317大电流可调稳压电路

1、 概述&#xff1a; D317是一款三端可调正稳压器集成电路&#xff0c;其输出电压范围是1.2V至37V&#xff0c;负载电流最大为1.5A。它的使用非常简单&#xff0c;仅需两个外接电阻来设置输出电压。此外&#xff0c;它的电压线性度和负载调整率也比标准的固定稳压器好。D317内…

通过联合部署DDoS高防和WAF提升网站防护能力

如果您的网站遭受的攻击既有流量型攻击&#xff0c;又混杂精巧的Web应用层攻击时&#xff08;例如SQL注入、跨站脚本攻击、命令注入等&#xff09;时&#xff0c;推荐您组合使用阿里云DDoS高防和Web 应用防火墙 WAF&#xff08;Web Application Firewall&#xff09;&#xff0…

【MySQL】事务管理 -- 详解

一、前言 CURD 不加控制&#xff0c;会有什么问题&#xff1f; CURD 满足什么属性&#xff0c;能解决上述问题&#xff1f; 买票的过程得是原子的。买票应该不能受互相的影响。买完票应该要永久有效。买前和买后都要是确定的状态。 什么是事务&#xff1f; 事务就是一组 DML…

2024-03-03 c++

&#x1f338; MFC进度条控件 | Progress Control 1。新建MFC项目&#xff08;基于对话框、静态库&#xff09; 2。添加控件&#xff0c;删除初始的3个多余控件 加1个progress control&#xff0c;修改其marquee为true&#xff0c;添加变量&#xff1a;变量名为test_progress。…

乡村教师的待遇会比城里的好吗

每次提到乡村教师&#xff0c;我们总会联想到那些坚守在偏远山区的教育工作者&#xff0c;他们默默无闻&#xff0c;为了乡村的孩子们奉献着自己的青春和热血。那么&#xff0c;乡村教师的待遇究竟如何呢&#xff1f;是否真的如外界所传闻的那般&#xff0c;比城里的教师还要好…

ip https证书360元买一年送一月

随着互联网的发展&#xff0c;不论是用户还是开发者&#xff0c;都越来越重视互联网环境的安全性。IP https证书是一种网络安全协议&#xff0c;用于保护网络通信的安全性和机密性。IP https数字证书是CA认证机构为只有公网IP地址&#xff0c;没有域名的站点颁发的数字证书&…

华为HarmnyOS TypeScript基础语法快速入门

华为HarmnyOS TypeScript基础语法快速入门 一、JavaScript、TypeScript、ArkTS二、TypeScript基础语法1. 基础类型2. 条件语句3. 函数4. 类5. 模块6. 迭代器 一、JavaScript、TypeScript、ArkTS ArkTS是HarmonyOS优选的主力应用开发语言。它在TypeScript&#xff08;简称TS&am…

信钰证券:四川黄金超50亿元解禁,紫金矿业等解禁股东浮盈超200%

本周A股限售股解禁规划不到400亿元&#xff0c;环比下降。 除掉新上市公司&#xff0c;本周共有43家公司限售股解禁&#xff0c;解禁数量28.91亿股&#xff0c;以最新收盘价计算&#xff08;下同&#xff09;&#xff0c;解禁市值387.66亿元。 其间&#xff0c;解禁市值超越1…

MASS/MM17批量复制物料描述

需求&#xff1a; 批量将日文环境中的物料描述复制到英文环境。 实现&#xff1a; 1.将日文环境下的物料描述下载至本地excel。 2.新建EXCEL&#xff0c;添加如下标题列&#xff0c;并将第一步下载下来的内容粘贴至对应的列&#xff0c;MANDT改为实际要更新的客户端&#xf…

基于Python的单词抽取测试工具

一、引言 在语言学习的过程中&#xff0c;单词量的多少是衡量一个人英语水平的重要指标。而如何快速扩充词汇量&#xff0c;掌握单词的汉语意思是重多师生的痛点和难点。为了帮助学习者有效地扩展词汇&#xff0c;巩固学习成果&#xff0c;我们在ChatGPT-4.0的帮助下&#xff…

Cookie和session 及Web相关工具

一 Cookie &#xff08;一&#xff09;介绍 Cookie 又称为"小甜饼”。类型为"小型文本文件”&#xff0c;指某些网站为了辨别用户身份而储存在用户本地终端&#xff08;Client Side&#xff09;上的数据&#xff08;通常经过加密&#xff09;。由网景公司的前雇员…

深入了解 Android 中的 FrameLayout 布局

FrameLayout 是 Android 中常用的布局之一&#xff0c;它允许子视图堆叠在一起&#xff0c;可以在不同位置放置子视图。在这篇博客中&#xff0c;我们将详细介绍 FrameLayout 的属性及其作用。 <FrameLayout xmlns:android"http://schemas.android.com/apk/res/androi…