当前位置:网站首页>链表 删除链表中的节点
链表 删除链表中的节点
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
}
}
边栏推荐
- Remove the mosaic, there's a way, attached with the running tutorial
- [competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
- [zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
- Work of the 15th week
- 2021mathorcupc topic optimal design of heat dissipation for submarine data center
- Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
- Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
- 可穿戴设备或将会泄露个人隐私
- Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
- Is it safe to open an account online? Who can I ask?
猜你喜欢

Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary

Encoding format for x86

Register the jar package as a service to realize automatic startup after startup

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

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
![[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application](/img/4a/1e83ea9e8e79c0ae7244a159943480.jpg)
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

Chitubox micromake l3+ slicing software configuration correspondence

Remove the mosaic, there's a way, attached with the running tutorial

Creo makes a mobius belt in the simplest way
随机推荐
Voiceprint Technology (V): voiceprint segmentation and clustering technology
Oracle function trigger
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
处理图片类库
Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Processing picture class library
Force buckle -104 Maximum depth of binary tree
Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics
JS tool function, self encapsulating a throttling function
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Arduino bootloader burning summary
将jar包注册为服务,实现开机自动启动
Learning notes of rxjs takeuntil operator
manhattan_ Slam environment configuration
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
x86电脑上下载debian的arm64的包
Learning notes of rxjs takeuntil operator
Encoding format for x86