当前位置:网站首页>链表 删除链表中的节点
链表 删除链表中的节点
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
}
}
边栏推荐
- Prediction of pumpkin price based on BP neural network
- [zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)
- [zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most
- SQL advanced
- 【mysql学习笔记21】存储引擎
- neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
- 匯付國際為跨境電商賦能:做合規的跨境支付平臺!
- [project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
- Neat Syntax Design of an ETL Language (Part 2)
- MySQL创建给出语句
猜你喜欢

Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts

Learning notes of rxjs takeuntil operator

Analysis on the thinking of 2022 meisai C question

Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)

neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)

Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

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

Solution to the problem of repeated startup of esp8266

Wearable devices may reveal personal privacy

Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
随机推荐
Learning notes of rxjs takeuntil operator
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
Japanese online notes for postgraduate entrance examination (9): composition template
Work of the 15th week
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
JS tool function, self encapsulating a throttling function
Oracle function trigger
MySQL source code reading (II) login connection debugging
Etcd教程 — 第四章 Etcd集群安全配置
Solution to the problem of repeated startup of esp8266
[buuctf.reverse] 117-120
[IOU] intersection over union
Neat Syntax Design of an ETL Language (Part 2)
Arduino bootloader burning summary
What are the PMP scores?
Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
manhattan_ Slam environment configuration
手机办理长城证券开户靠谱安全吗?
Register the jar package as a service to realize automatic startup after startup