当前位置:网站首页>链表 删除链表中的节点
链表 删除链表中的节点
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
}
}
边栏推荐
- Cassava tree disease recognition based on vgg16 image classification
- Puzzle (019.2) hexagonal lock
- Compare and explain common i/o models
- Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
- Neat Syntax Design of an ETL Language (Part 2)
- Fluent creates, reads and writes JSON files
- Processing picture class library
- 2021mathorcupc topic optimal design of heat dissipation for submarine data center
- MySQL创建给出语句
- [MySQL learning notes 20] MySQL architecture
猜你喜欢

How much money have I made by sticking to fixed investment for 3 years?
![[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition](/img/b4/84c30ed112c4dffd8d51697b2f4a4f.jpg)
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition

Lvs-dr mode single network segment case

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points

x86电脑上下载debian的arm64的包

CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded

Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics

Wearable devices may reveal personal privacy

Simple waterfall effect

2021mathorcupc topic optimal design of heat dissipation for submarine data center
随机推荐
SQL advanced
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Title B of the certification cup of the pistar cluster in the Ibagu catalog
Wearable devices may reveal personal privacy
Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts
瑞萨RA系列-开发环境搭建
8、智慧交通项目(1)
JS tool function, self encapsulating a throttling function
Solution to the problem of repeated startup of esp8266
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
The problem of wirengpi program running permission
How to "transform" small and micro businesses (II)?
Puzzle (019.2) hexagonal lock
Register the jar package as a service to realize automatic startup after startup
【mysql学习笔记22】索引
Fluent creates, reads and writes JSON files
[smart agriculture program] smart agriculture small program project is currently popular.
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
将jar包注册为服务,实现开机自动启动
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)