当前位置:网站首页>Linked list delete nodes in the linked list
Linked list delete nodes in the linked list
2022-06-25 10:06:00 【Morris_】
LC Delete the nodes in the list
Please write a function , be used for Delete a specific node in the single linked list . When designing functions, you need to pay attention to , You can't access the head node of the linked list head , Direct access only The node to be deleted .
The topic data ensures that the nodes to be deleted Not the end node .
Input :head = [4,5,1,9], node = 5
Output :[4,1,9]
explain : Specifies that the value in the linked list is 5 Second node of , So after calling your function , The list should be 4 -> 1 -> 9
public class ListNode {
/// Node values
public var val: Int
/// next node
public var next: ListNode?
/// The node value is passed in during initialization , On initialization next The node is nil
public init (_ val: Int) {
self.val = val
self.next = nil
}
}
Ideas :
General , If you want to delete 5 , Our first thought is to 5 The successor nodes of the predecessor nodes of the node point to 5 Successor node .
In short, it means that 4 The node of next Pointer to 1, Then delete 5 Of next Just a pointer , As shown in the figure below, the line is divided
But the problem is that we don't know 5 Precursor node of this node , because ListNode There is no node stored in the class pre node , Only saved next node .

Another way of thinking , If we change the value of the current node to the value of the next node , Then the current node's next Pointer to lower node , It is as big as expected .
swift Realization
/// Node class
public class ListNode {
/// Node values
public var val: Int
/// next node
public var next: ListNode?
/// The node value is passed in during initialization , On initialization next The node is 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
}
}
边栏推荐
- pmp考试题型需要注意哪些?
- Solution to the problem of repeated startup of esp8266
- Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
- Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
- Rxjs TakeUntil 操作符的学习笔记
- Kotlin advanced generic
- View. post VS Handler. Differences and usage scenarios of post
- PHP obtains the IP address, and the apache2 server runs without error
- What should be paid attention to in PMP examination?
- Reza RA series - development environment construction
猜你喜欢

CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束

Exception: gradle task assemblydebug failed with exit code 1

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

manhattan_ Slam environment configuration

可穿戴设备或将会泄露个人隐私

Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?

Vscode attempted to write the procedure to a pipeline that does not exist

x86的编码格式

i++ 和 ++i的真正区别

ScheduleMaster分布式任务调度中心基本使用和原理
随机推荐
How to make a self-made installer and package the program to generate an installer
Flutter dialog: cupertinoalertdialog
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
瑞吉外卖项目(二)
[MySQL learning notes 20] MySQL architecture
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
Modbus协议与SerialPort端口读写
Swift recursively queries the array for the number closest to the specified value
NetCore性能排查
Etcd tutorial - Chapter 4 etcd cluster security configuration
MongoDB的原理、基本使用、集群和分片集群
Processing picture class library
Jetpack compose layout (III) - custom layout
How to develop wechat applet? How to open a wechat store
MySQL create given statement
Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow
How do wechat applets make their own programs? How to make small programs on wechat?
clang frontend command failed with exit code 250
广发证券靠谱吗?是否合法?开股票账户安全吗?
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!