当前位置:网站首页>链表 删除链表中的节点
链表 删除链表中的节点
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
}
}
边栏推荐
- 在指南针上面开户好不好,安不安全?
- [smart agriculture program] smart agriculture small program project is currently popular.
- js工具函数,自己封装一个节流函数
- Compare and explain common i/o models
- The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
- Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points
- Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)
- 2021mathorcupc topic optimal design of heat dissipation for submarine data center
- [Ruby on rails full stack course] course directory
- Arduino bootloader burning summary
猜你喜欢
Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
Analysis on the thinking of 2022 meisai C question
Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
瑞萨RA系列-开发环境搭建
203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
Unique Wulin, architecture selection manual (including PDF)
Ruiji takeout project (II)
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Etcd tutorial - Chapter 4 etcd cluster security configuration
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
随机推荐
What should be paid attention to in PMP examination?
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Ruiji takeout project (II)
[buuctf.reverse] 117-120
【mysql学习笔记22】索引
I put a two-dimensional code with rainbow candy
Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
Data-driven anomaly detection and early warning of 21 May Day C
Is it safe for Huatai Securities to open an account on it? Is it reliable?
C语言刷题随记 —— 猴子吃桃
22 mathematical modeling contest 22 contest C
Shuttle JSON, list, map inter transfer
vscode试图过程写入管道不存在
Cocopod error failed: undefined method `map 'for nil:nilclass
Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
How to "transform" small and micro businesses (II)?
2021mathorcupc topic optimal design of heat dissipation for submarine data center
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded