当前位置:网站首页>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 .
边栏推荐
- Resource arrangement
- Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
- Traverse the array and delete an element until it is deleted
- Opencv 17 face recognition
- Retrofit easy to use
- [life science] DNA extraction of basic biological experiments
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- A wechat app for shopping
- Ijkplayer source code - rendering
- Rounding in JS
猜你喜欢

Principle and steps of principal component analysis (PCA)
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
![[reading papers] visual convolution zfnet](/img/01/4181f19b2d24b842488522c2001970.jpg)
[reading papers] visual convolution zfnet

Use of OpenCV 12 findcircuits and drawcircuits

微信云开发粗糙理解
![[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 drawing 12- importance of chart notes](/img/9c/c610c6f9d08952aece97f788ae35a7.jpg)
[data analysis and visualization] key points of data drawing 12- importance of chart notes

Open source video recolor code

Matlab: find the inner angle of n-sided concave polygon

Data processing in detailed machine learning (II) -- Feature Normalization
随机推荐
too old resource version,Code:410
[reading papers] comparison of deeplobv1-v3 series, brief review
Model prediction of semantic segmentation
Surpass the strongest variant of RESNET! Google proposes a new convolution + attention network: coatnet, with an accuracy of 89.77%!
Introduction to facial expression recognition system - Technical Paper Edition
Use of OpenCV 12 findcircuits and drawcircuits
The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
Matlab: find the inner angle of n-sided concave polygon
[data analysis and visualization] key points of data mapping 7- over mapping
Opencv 10 brightness contrast adjustment
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
Opencvshare4 and vs2019 configuration
01 初识微信小程序
Graduation project - campus old thing recycling system based on stm32
01 initial knowledge of wechat applet
专业的数据库管理软件:Valentina Studio Pro for Mac
Graph theory, tree based concept
04 route jump and carry parameters
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial