当前位置:网站首页>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
}
}
边栏推荐
- Puzzle (019.2) hexagonal lock
- 独步武林,架构选型手册(包含 PDF)
- How much does a small program cost? How much does a small program cost? It's clear at a glance
- WebApi性能优化
- Reasons for Meiye to choose membership system
- Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
- 力扣-104. 二叉树的最大深度
- Shuttle JSON, list, map inter transfer
- What are the PMP scores?
- Oracle function trigger
猜你喜欢

How to make a self-service order wechat applet? How to do the wechat order applet? visual editing

Wearable devices may reveal personal privacy

Jetpack compose layout (I) - basic knowledge of layout

Get started quickly with jetpack compose Technology

Redis(一)原理与基本使用

Flutter dialog: cupertinoalertdialog

Rxjs TakeUntil 操作符的学习笔记

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial

How to "transform" small and micro businesses (I)?
随机推荐
Cocopod error failed: undefined method `map 'for nil:nilclass
BUG-00x bug description + resolve ways
Get started quickly with jetpack compose Technology
[MySQL learning notes 22] index
Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
Minio基本使用与原理
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
Mysql 源码阅读(二)登录连接调试
Redis(一)原理与基本使用
i++ 和 ++i的真正区别
[MySQL learning notes 21] storage engine
字符串 最长公共前缀
Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
8、智慧交通项目(1)
How much money have I made by sticking to fixed investment for 3 years?
Jetpack compose layout (II) - material components and layout
‘Flutter/Flutter. h‘ file not found
What functions should smart agriculture applet system design have
Kotlin advanced generic
链表 删除链表中的节点