leetcode 237. Delete Node in a Linked List(链表中删除节点)

Leetcode刷题笔记-linkedlist 369.Plus One Linked List p1指向最后面非9的数字。p2指向结尾。 class Solution(object): def plusOne(self, head): # two pointers dummy = ListNode(0) dummy.next = head p1 = p2 = ... 阅读详情

There is a singly-linked list head and we want to delete a node node in it.

You are given the node to be deleted node. You will not be given access to the first node of head.

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

The value of the given node should not exist in the linked list.
The number of nodes in the linked list should decrease by one.
All the values before node should be in the same order.
All the values after node should be in the same order.
Custom testing:

For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
We will build the linked list and pass the node to your function.
The output will be the entire list after calling your function.
在这里插入图片描述
看题目有点茫然,不知道让干嘛。
然后分析一下是这样的:
测试平台有一个单向链表(但是不会给你),它会调用deleteNode(node)函数(你负责实现这个函数),
调用完之后node这个节点会被删掉。

所以你知道的信息只有node这个节点,链表本身是什么你并不知道。
node不是tail节点。

思路:

我们负责实现的是deleteNode这个函数,传来的信息只有要删除的节点node.
同时还有node.next, node.val这些信息。

由于没有前一个节点,并不能删掉node本身,但可以删掉下一节点。
题目中也说了,删除node并不意味着要把node移出内存,只是要求它的值不在链表中出现,
链表保持原顺序即可,
而且node不是tail节点,这说明我们可以删除下一节点,然后把node的值变为下一节点的值即可。

public void deleteNode(ListNode node) {
    ListNode nextNode = node.next;
    int nextVal = nextNode.val;
    node.next = nextNode.next;
    nextNode.next = null;
    node.val = nextVal;
}
Leetcode 237. Delete Node in a Linked List-删除链表的指定节点,不给链表的头节点 Write a function todelete a nodein a singly-linked list. You willnotbe given access to theheadof the list, instead you will be given access tothe node to be deleteddirectly. It isguaranteedthat the node to be deleted isnot a tail nodein the li... 阅读详情

相关推荐

抖音视频数据获取实战:从API调用到热门内容挖掘

通过抖音API获取视频数据,企业可快速构建数据驱动的运营体系。本文提供了从权限申请到商业落地的全流程指南,建议结合自身业务扩展功能。如有技术问题,欢迎​​评论​​区留言讨论!#抖音开发 #数据挖掘 #短视频运营 #API调用 #Python实战。

WBKJ_Noah_的博客 6408

Given a link list,remove the n th node from the end of list and return its head.

题目: Given a link list,remove the n th node from the end of list and return its head. Note: Given n will always be valid. Try to do this in one pass. 给一个单链表,让你删除它的倒数第n个结点。 这个题目很简单,可以先遍历一遍求出...

Monster_ii的博客 778

手把手教会你用yolov8从环境配置到检测任务实战

做本文章前,我浏览过很多网上资料,我发现他们总是写的含糊不清,而且实际操作起来就会有各种各样的问题,要么就是根本不适合一些小白,所以我在这里写一篇完全针对小白的yolo从入门到实战。看完本文章后大家都可以实现下图效果,数字识别是一种比较简单的检测任务,其他识别任务学完本例也能够一通百通,大家边做边学,更好的体会yolo的神奇之处。

KevenDuan的博客 2873

[题集]ADS13 Randomized Algorithms

1.Let a=(a1,a​2,…,a​i,…,aj,…,an) denote the list of elements we want to sort. In the quicksort algorithm, if the pivot is selected uniformly at random. Then any two elements get compared at most once and the probability of ai and a​j being compared is 2/(j

HGGshiwo的博客 2965

单细胞分析R包monocle3服务器安装教程

由于centos7系统yum只能安装到低版本gdal,而monocle3依赖包需要 gdal>2 版本,从gdal官网下载2.4.4版本编译安装。monocle3的安装是通过cole-trapnell-lab安装的,该包挂在github上,需要通过github先安装leidenbase包,需要提前安装devtools包下载工具。接着利用BiocManager安装相应的依赖包,由于这些包的特殊性,所以不要切换到国内Bio_Mirror镜像,否则会找不到,如果一次不成功,缺啥补啥即可。运行 ldconfig。

qq_36608036的博客 1万+

AUTOSAR E2E Protocol实战:Profile1到Profile5的配置差异与最佳实践

本文深入解析AUTOSAR E2E Protocol中Profile1到Profile5的核心配置差异与适用场景。针对经典CAN、CAN FD及以太网等不同总线,详细对比了计数器、CRC、DataID等关键参数,并结合实战代码与配置示例,为工程师根据功能安全等级、数据长度与带宽限制选择最佳Profile提供了清晰的选型路径与工程实践指南。

milk5的博客 946

02-线性结构3 Reversing Linked List (25分)

02-线性结构3 Reversing Linked List (25分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→

Gyibin的博客 196

STM32F429移植LVGL9.3.0:从源码配置到双缓冲优化实战

本文详细介绍了如何在STM32F429平台上移植LVGL 9.3.0图形库,涵盖从源码准备、工程配置、显示驱动适配到双缓冲优化等关键步骤。重点解析了利用STM32F429的硬件特性实现高效内存管理和流畅显示性能的实战技巧,为嵌入式GUI开发提供完整解决方案。

q6r7s8t9的博客 1145

237. 删除链表中的节点

题目描述:只给要删除的节点,不给head,要求调用函数后原链表能删除该节点 思路:既然不能干掉自己,那就变成儿子,在干掉儿子

m0_46566336的博客 189

Win10系统安装Ubuntu虚拟机,这回真的是零基础!(结尾附NS3的配置)

Win10系统安装Ubuntu虚拟机(结尾附NS3的配置) 序幕 本人现在还在念大二,有一门很有趣而且老师讲得也不错的课程《计算机网络原理》。这周老师布置了一个实验《NS3网络模拟》,为了详细记录本次实验过程,顺便也便于自己复习,就写了这篇博客,希望能够对你有帮助! NS3是什么呢?实际上就是一门算法,和计算机网络有关的。不过在我们日常使用的Windows系统上去做这个实验是行不通的,这个实验一定要在Linux系统上进行模拟才能玩的下去,作为一名 Windows选手对此表示不爽但是没办法,为了实验也只

灼热之翼的博客 7386

微信小程序导航栏滑动一定距离后固定到顶部

接上一篇文章的内容,通常在一个页面中部会有一个导航栏查看不同的内容,这篇文章旨在实现在页面下滑的过程中导航栏自动固定到顶部以便我们阅读过程中的切换。 下滑一段距离后自动固定到顶部 首先,思考这个功能应该是怎么实现的: 在页面下滑的过程中,我们希望系统应当能识别下滑了多少距离,当原本的导航栏逐渐被遮住以后(遮住意味着下滑了特定距离),触发这个功能,执行另一种样式,将导航栏固定在页面顶端,同时如果我们将页面往上滑,系统能识别到原来的导航栏能露出来了(即距离顶端的高度小于一个特定的值),这时候又执行最初的样式

Oblivision_Geek的博客 4033

LeetCode 237. 删除链表中的节点 Delete Node in a Linked List

一、中文版 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。 现有一个链表 --head =[4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为5的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -&...

三妹的博客 343

ESP32-C3模组上跑通OTA升级(3)

ESP32-C3模组上跑通OTA升级(3)

phmatthaus的专栏 1353

LeetCode 237 Delete Node in a Linked List(在链表中删除节点)(Linked List)

翻译给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点。假设这个链表是1 -> 2 -> 3 -> 4,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为1 -> 2 -> 4。原文Write a function to delete a node (except the tail) in a singly linked list, given only

nomasp 4972

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Leetcode 237.删除链表中的节点(Delete Node in a Linked List)

Leetcode 237.删除链表中的节点 1 题目描述(Leetcode题目链接)   请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。   现有一个链表 – head = [4,5,1,9],它可以表示为: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你...

qq_39378221的博客 185

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode 237. Delete Node in a Linked List 删除链表的节点

1.题目 编写一个函数来删除单链表中的节点(尾部除外),只允许访问该节点。 示例 : Input: head = [4,5,1,9], node = 5 Output: [4,1,9] 2.思路 只需的时间复杂度可以删除该节点,步骤如下: 令该节点的值为下一个节点的值; 该节点指向下一个节点的next位置。 把下一个节点删除。 例如 i->j 删除节点i 1...

拾遗 262

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

leetcode 237. Delete Node in a Linked List(删除链表中的特定节点)

题目要求(技巧题) 编写一个函数来删除单链表中的节点(尾部除外),只允许访问该节点。 实例 // Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 ...

爱喝舒化奶的博客 585

[Leetcode] 237. Delete Node in a Linked List 给出链表中的某个节点,删除该节点

237.Delete Node in a Linked List (题目链接) Easy Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list --head =[4,5,1,9], which looks like following: Example 1: Input: head ...

热爱编程,热爱技术 201

【leetcode】237. 删除链表中的节点( Delete Node in a Linked List )

题目描述 【leetcode】237. 删除链表中的节点( Delete Node in a Linked List ) 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你只能够获得被要求被删除的节点(博主注:也就是说,你无法获得被删除节点的上一个节点)。 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 ...

Liber-coder的博客 201

[Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30402343的博客 127

237[leetcode]删除链表中的节点(Delete Node in a Linked List)

(一)题目描述 (二)思想方法 将后一个节点的值赋给当前节点,删除后一个节点 (三)代码实现 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NUL...

guaiguaitinghua的博客 211

Leetcode 237. Delete Node in a Linked List 删除链表中的节点

题目:请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 -> 9 示例 1:输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变...

zhangzhetaojj的博客 174

【LeetCode】237. Delete Node in a Linked List 删除链表中的节点(Easy)(JAVA)

【LeetCode】237. Delete Node in a Linked List 删除链表中的节点(Easy)(JAVA) 题目地址: https://leetcode.com/problems/delete-node-in-a-linked-list/ 题目描述: Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instea

qq_16927853的博客 217

leetcode 237 删除链表中的节点(Delete Node in a Linked List) python 最简

''' Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list -- head = [4,5,1,9], which looks like following: Example 1: Inpu...

Jasmine_dream 361

LeetCode #237. Delete Node in a Linked List 删除链表中的节点

https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题。 题目没有给head,心想没有head我怎么才能找到要删除的值对应的节点呢? 仔细一看,题中函数的参数给的不是值,而是要删除的节点node。反而降低了解题难度: 1. 把node.next的值赋给node 2. 把node.nex...

weixin_30339969的博客 161

Leetcode|237.Delete Node in a Linked List删除链表中的节点.java

题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。 现有一个链表 – head = [4,5,1,9],它可以表示为: 示例 : 输入:head = [4,5,1,9], node = 5 输出:[4,1,9] 解释:给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 输入:head = [4,5,1,9], node = 1 输出:[4,5,9] 解释:给定你链表中值为 1 的第三个节

梦晨涌京 158
上一篇: leetcode 19. Remove Nth Node From End of List(删除链表从结尾开始的第N个节点)
下一篇: C++ 实现ONNX语义分割(TopFormer)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值