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

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 = headslow, fast = ret_head, headwhile fast and fast.next:fast = fast.next.nextslow = slow.next# delete slow.nextslow.next = slow.next.nextreturn ret_head.next

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/718733.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/718733.shtml
英文地址,請注明出處:http://en.pswp.cn/news/718733.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python中的類與對象(3)

目錄 一. 類的多繼承 二. 類的封裝 三. 類的多態 四. 類與對象綜合練習&#xff1a;校園管理系統 一. 類的多繼承 在&#xff08;2&#xff09;第四節中我們介紹了什么是類的繼承&#xff0c;在子類的括號里面寫入要繼承的父類名。上一節我們只在括號內寫了一個父類名&…

新手淘寶開店如何引流

對于新手淘寶賣家來說&#xff0c;引流是開店過程中最為關鍵的一環。如何吸引潛在客戶進入店鋪&#xff0c;提高商品的曝光率和銷量&#xff0c;是每個新手賣家都面臨的挑戰。本文將為你提供新手淘寶開店的引流攻略&#xff0c;幫助你從零開始掌握實用的引流技巧。 一、優化店…

C++的類型轉換

1.C語言中的類型轉換 在C語言中&#xff0c;如果賦值運算符左右兩側類型不同&#xff0c;或者形參與實參類型不匹配&#xff0c;或者返回值類型與接收返回值類型不一致時&#xff0c;就需要發生類型轉化&#xff0c;C語言中總共有兩種形式的類型轉換&#xff1a;隱式類型轉換和…

【機器人最短路徑規劃問題(柵格地圖)】基于模擬退火算法求解

代碼獲取方式&#xff1a;QQ&#xff1a;491052175 或者 私聊博主獲取 基于模擬退火算法求解機器人最短路徑規劃問題&#xff08;柵格地圖&#xff09;的仿真結果 仿真結果&#xff1a; 初始解的路徑規劃圖 收斂曲線&#xff1a; 模擬退火算法求解的路徑規劃圖 結論&#xff…

Ubuntu20安裝zabbix-agent2,對接zabbix 6.4

在Ubuntu 20.04 LTS上安裝Zabbix Agent 2并與Zabbix Server 6.4對接&#xff0c;請按照以下步驟操作&#xff1a; 更新系統&#xff1a; sudo apt update sudo apt upgrade 添加Zabbix官方倉庫&#xff1a; 首先&#xff0c;需要將Zabbix的官方存儲庫添加到你的系統中以獲取Za…

C#面:常用的 異常類 有哪些

異常類是用于處理程序運行時出現的錯誤或異常情況的類。 C# 提供了一些內置的異常類&#xff0c;常用的包括&#xff1a; System.Exception&#xff1a;所有異常類的基類&#xff0c;可以用于捕獲所有類型的異常。System.SystemException&#xff1a;表示系統級別的異常&…

【了解SpringCloud Gateway微服務網關】

曾夢想執劍走天涯&#xff0c;我是程序猿【AK】 目錄 簡述概要知識圖譜什么是SpringCloudGateway功能特征應用場景核心概念配置文件工作原理路由謂詞工廠&#xff08;內置的&#xff09;[After 路由謂詞工廠](https://docs.spring.io/spring-cloud-gateway/docs/current/refere…

Mysql運維篇(七) 部署MHA--完結

一路走來&#xff0c;所有遇到的人&#xff0c;幫助過我的、傷害過我的都是朋友&#xff0c;沒有一個是敵人。如有侵權&#xff0c;請留言&#xff0c;我及時刪除&#xff01; 一、MHA軟件構成 Manager工具包主要包括以下幾個工具&#xff1a; masterha_manger 啟…

【C++】多態深入分析

目錄 一&#xff0c;多態的原理 1&#xff0c;虛函數表與虛函數表指針 2&#xff0c;原理調用 3&#xff0c;動態綁定與靜態綁定 二&#xff0c;抽象類 三&#xff0c;單繼承和多繼承關系的虛函數表 1&#xff0c;單繼承中的虛函數表 2&#xff0c;多繼承中的虛函數表 …

“編碼迷宮中的探險者:探索程序員職業賽道的無限可能“

在這個信息技術飛速發展的時代&#xff0c;程序員的職業賽道就像是一座錯綜復雜的迷宮&#xff0c;它既充滿了挑戰&#xff0c;又蘊藏著無限的機遇。這座迷宮中&#xff0c;有前端的美麗花園&#xff0c;后端的黑暗洞穴&#xff0c;還有數據科學的神秘密室。每一條路徑都有其獨…

內網搭建mysql8.0并搭建主從復制詳細教程!!!

一、安裝mysql 1.1 mysql下載鏈接&#xff1a; https://downloads.mysql.com/archives/community/ 1.2 解壓包并創建相應的數據目錄 tar -xvf mysql-8.2.0-linux-glibc2.28-x86_64.tar.xz -C /usr/local cd /usr/local/ mv mysql-8.2.0-linux-glibc2.28-x86_64/ mysql mkdir…

Python繪圖-9餅圖(上)

餅圖&#xff08;Pie Chart&#xff09;是一種用于表示數據分類和相對大小的可視化圖形。在餅圖中&#xff0c;整個圓形代表數據的總和&#xff0c;而圓形內的各個扇形則代表不同的分類或類別&#xff0c;扇形的面積大小表示該類別在整體中所占的比例。餅圖通常用于展示數據的分…

FW, IPS, IDS

文章目錄 FW (Firewall, 防火墻)IPS (Intrusion Prevention System, 入侵防御系統)IDS (Intrusion Detection System, 入侵檢測系統)IDS vs. FWIPS FW (Firewall, 防火墻) 產品定位&#xff1a; 防火墻的主要作用是進行網絡訪問控制。它充當網絡的門衛&#xff0c;控制進入和離…

《人間值得》讀書筆記

人的一生說短不短&#xff0c;說長不長。蕓蕓眾生&#xff0c;為了生活努力的掙扎&#xff0c;太少的人能衣食無憂&#xff0c;所以我們每天為了碎銀幾兩&#xff0c;為了生活奔波。 《人間值得》的主人公是一個90歲的老奶奶&#xff0c;她的生活經歷很豐富&#xff0c;她的人…

ObjectProvider學習

簡介 ObjectProvider 是 Spring Framework 5.0 之后引入的一個新接口&#xff0c;它提供了一個靈活的方式來訪問由 Spring 容器管理的 Bean。ObjectProvider 提供了一種更加類型安全的方式來查找和注入依賴項&#xff0c;同時還支持 Null 值的處理以及延遲初始化。 ObjectProv…

Window部署Jaeger

參考&#xff1a;windows安裝使用jaeger鏈路追蹤_windows安裝jaeger-CSDN博客 下載&#xff1a;Releases jaegertracing/jaeger GitHub Jaeger – Download Jaeger 目錄 1、安裝nssm 2、安裝運行 elasticsearch 3、安裝運行 3.1部署JaegerAgent 3.2部署JaegerCollec…

【全志D1-H 哪吒開發板】Debian系統安裝調教和點燈指南

全志D1-H開發板【哪吒】使用Deabian系統入門 特別說明&#xff1a; 因為涉及到操作較多&#xff0c;博文可能會導致格式丟失 其中內容&#xff0c;會根據后續使用做優化調整 目錄&#xff1a; 參考資料固件燒錄啟動調教點燈問題 〇、參考資料 官方資料 開發板-D1開發板【…

C++:函數模板整理

函數模板: 找到函數相同的實現思路&#xff0c;區別于函數的參數類型。 使用函數模板使得函數可容納不同類型的參數實現函數功能&#xff0c;而不是當類型不同時便編譯大量類型不同的函數&#xff0c;產生大量重復代碼和內存占用 函數模板格式&#xff1a; template<typ…

[Vulnhub]靶場 Red

kali:192.168.56.104 主機發現 arp-scan -l # arp-scan -l Interface: eth0, type: EN10MB, MAC: 00:0c:29:d2:e0:49, IPv4: 192.168.56.104 Starting arp-scan 1.10.0 with 256 hosts (https://github.com/royhills/arp-scan) 192.168.56.1 …

ARM64匯編02 - 寄存器與指令基本格式

最近的文章可能會有較多修改&#xff0c;請關注博客哦 異常級別 ARMv8處理器支持4種異常等級&#xff08;Exception Level&#xff0c;EL&#xff09;。 EL0 為非特權模式&#xff0c;用于運行應用程序&#xff0c;其他資源訪問受限&#xff0c;權限不夠。 EL1 為特權模式&…