当前位置:网站首页>链表 删除链表中的节点
链表 删除链表中的节点
2022-06-25 09:37:00 【Morris_】
LC 删除链表中的节点
请编写一个函数,用于 删除单链表中某个特定节点 。在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 。
题目数据保证需要删除的节点 不是末尾节点 。
输入:head = [4,5,1,9], node = 5
输出:[4,1,9]
解释:指定链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9
public class ListNode {
/// 节点值
public var val: Int
/// next节点
public var next: ListNode?
/// 初始化时候传入节点值,初始化时next节点为nil
public init (_ val: Int) {
self.val = val
self.next = nil
}
}
思路:
一般的,如果要删除 5 ,我们首先想到的是将 5 的节点的前驱节点的后继节点指向5的后继节点。
简而言之就是将4的节点的next指针指向1,然后删除5的next指针即可,如下图分割线上部分
但是有个问题就是我们不知道5这个节点的前驱节点,因为ListNode类里面没有保存节点的pre节点,只保存了next节点。

换一个思路,如果我们将当前的节点的值改成下一个节点的值,然后将当前节点的next指针指向下下节点,就大到预期效果了。
swift 实现
/// 节点类
public class ListNode {
/// 节点值
public var val: Int
/// next节点
public var next: ListNode?
/// 初始化时候传入节点值,初始化时next节点为nil
public init (_ val: Int) {
self.val = val
self.next = nil
}
}
class Solution {
func deleteNode(_ node: ListNode?) {
var tempNode = node?.next
node?.val = (node?.next!.val)!
node?.next = node?.next?.next
tempNode?.val = 0
tempNode = nil
}
}
边栏推荐
- Chitubox micromake l3+ slicing software configuration correspondence
- [MySQL learning notes 22] index
- [IOU] intersection over union
- MySQL source code reading (II) login connection debugging
- Is it safe to open an account online? Who can I ask?
- Use evo
- With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- 广发证券靠谱吗?是否合法?开股票账户安全吗?
- PMP考试多少分算通过?
猜你喜欢

Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?

How to "transform" small and micro businesses (II)?

Exception: gradle task assemblydebug failed with exit code 1
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)

独步武林,架构选型手册(包含 PDF)

Reasons for Meiye to choose membership system

匯付國際為跨境電商賦能:做合規的跨境支付平臺!

Etcd tutorial - Chapter 4 etcd cluster security configuration

Puzzle (019.2) hexagonal lock

瑞吉外卖项目(二)
随机推荐
Is it safe for Huatai Securities to open an account on it? Is it reliable?
Set the location permission in the shutter to "always allow"
Title B of the certification cup of the pistar cluster in the Ibagu catalog
Armbian version name comparison
富时A50开户什么地方安全
oracle 函数 触发器
在指南针上面开户好不好,安不安全?
Arduino bootloader burning summary
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
How to delete a blank page that cannot be deleted in word
【mysql学习笔记20】mysql体系结构
Etcd教程 — 第四章 Etcd集群安全配置
x86电脑上下载debian的arm64的包
Exception: gradle task assemblydebug failed with exit code 1
8、智慧交通项目(1)
How to "transform" small and micro businesses (I)?
2021mathorcupc topic optimal design of heat dissipation for submarine data center
Rxjs TakeUntil 操作符的学习笔记
在指南针上面开股票账户好不好,安不安全?
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course