当前位置:网站首页>Linked list: delete the penultimate node of the linked list
Linked list: delete the penultimate node of the linked list
2022-06-13 02:43:00 【Zeng Qiang】
List of articles
subject
https://leetcode-cn.com/problems/SLwz0R/
Define a linked list , Please delete the penultimate in the linked list N Nodes , Output a new linked list .
Input list 1->2->3->4->5 , n = 2
Output :1->2->3->5
What is a linked list
A list of nodes connected by pointers .
The time complexity of finding a node is O(N)
newly added 、 The time complexity of deleting a node is O(1)
Their thinking
Violence enumeration
We can traverse the linked list twice , Complete deletion of section n Tasks of nodes . The first traversal obtains the length of the linked list n, The second traversal will traverse to the reciprocal n+1 Stop when , Then count down n+1 The node of is modified to point to the reciprocal n-1 The node of .
Double pointer
The title requires us to traverse the linked list once and delete the n Nodes , So we have to find other ways .
We define a sentinel node and two pointers , The sentinel node is used to avoid the situation that the node is empty .
The two pointers are one before and one after . Divide into 3 Step :
First step , Move the front pointer first n Step .
The second step , Move the front pointer and the back pointer at the same time , Until the previous pointer points to null , Stop moving .
The third step , The next node of the modified pointer points to , Delete node . Because the pointer just points to the reciprocal n+1 Nodes .
Code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode frontNode = head;
ListNode backNode = dummyNode;
for(int i = 0; i < n; ++i) {
frontNode = frontNode.next;
}
while(frontNode != null) {
frontNode = frontNode.next;
backNode = backNode.next;
}
backNode.next = backNode.next.next;
return dummyNode.next;
}
}
summary
This question is used to examine the linked list , We define a sentinel node to optimize the deletion of head nodes . Two fast and slow pointers are defined to retrieve the deleted node .
边栏推荐
- Implementing fillet in custom dialog
- 微信云开发粗糙理解
- [dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
- Exam23 named windows and simplified paths, grayscale conversion
- redis.conf总配置详解
- Use of OpenCV 11 kmeans clustering
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- Graph theory, tree based concept
- Rough understanding of wechat cloud development
- [reading some papers] introducing deep learning into the public horizon alexnet
猜你喜欢

A wechat app for shopping

Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL

在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容
![[data analysis and visualization] key points of data drawing 3- spaghetti map](/img/3d/ea832e67d22c62b07dc46cf49e50ba.jpg)
[data analysis and visualization] key points of data drawing 3- spaghetti map
![[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face](/img/e4/a25716ae7aa8bdea64eb9314ca2cc7.jpg)
[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face

A real-time target detection model Yolo
![[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2](/img/a1/151d2afe6d7f0bd95fe93fc80f633e.jpg)
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2
![[data analysis and visualization] key points of data mapping 7- over mapping](/img/ae/d4e251b37ec4857c99f738ca981092.jpg)
[data analysis and visualization] key points of data mapping 7- over mapping

Laravel permission export

Summary of innovative ideas of transformer model in CV
随机推荐
vant实现移动端的适配
[dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
Change tax for 2
How did you spend your winter vacation perfectly?
[reading papers] deep learning face representation from predicting 10000 classes. deepID
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
Simple use of leaflet - offline map scheme
Fast Color Segementation
PCR validation of basic biological experiments in [life sciences]
05 tabBar导航栏功能
Graph theory, tree based concept
Opencvsharp4 handwriting recognition
Uni app Foundation
Opencv 9 resize size change rotate rotate blur mean (blur)
04路由跳转并携带参数
OneNote User Guide (1)
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2
[data analysis and visualization] key points of data drawing 3- spaghetti map
FFmpeg原理
Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)