当前位置:网站首页>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
}
}
边栏推荐
- How do wechat sell small commodity programs do? How to open wechat apps to sell things?
- [buuctf.reverse] 117-120
- Grabcut image segmentation in opencv
- What are the PMP scores?
- Cocopod error failed: undefined method `map 'for nil:nilclass
- Swift recursively queries the array for the number closest to the specified value
- Encoding format for x86
- 瑞吉外卖项目(二)
- Rxjs TakeUntil 操作符的学习笔记
- How to do the wechat selling applet? How to apply for applets
猜你喜欢
How to develop wechat applet? How to open a wechat store
Unique Wulin, architecture selection manual (including PDF)
Flutter dialog: cupertinoalertdialog
【mysql学习笔记21】存储引擎
Kotlin advanced generic
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
8. Intelligent transportation project (1)
i++ 和 ++i的真正区别
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Modbus协议与SerialPort端口读写
随机推荐
How to make small programs on wechat? How to make small programs on wechat
8、智慧交通项目(1)
Guiding principle - read source code
Etcd教程 — 第四章 Etcd集群安全配置
i++ 和 ++i的真正区别
可穿戴设备或将会泄露个人隐私
SparseArray details
Methodchannel of flutter
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
Neat Syntax Design of an ETL Language (Part 2)
What are the PMP scores?
Kotlin advanced - class
如何自制一个安装程序,将程序打包生成安装程序的办法
How to make a self-made installer and package the program to generate an installer
js工具函数,自己封装一个节流函数
[buuctf.reverse] 117-120
NFC read / write mode development - book summary
Processing picture class library
Neat Syntax Design of an ETL Language (Part 2)
Flutter dialog: cupertinoalertdialog