当前位置:网站首页>[leetcode 19] delete the penultimate node of the linked list
[leetcode 19] delete the penultimate node of the linked list
2022-04-23 06:24:00 【Don't steal my energy】
Title Description
I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Example 1:
Input :head = [1,2,3,4,5], n = 2
Output :[1,2,3,5]
Example 2:
Input :head = [1], n = 1
Output :[]
Example 3:
Input :head = [1,2], n = 1
Output :[1]
Advanced : Can you try a scan implementation ?
Their thinking
1. Two iterations , First record penultimate N A position in the positive order of the linked list . But traversed the linked list twice .
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *p=head;
ListNode *r=head;
int i=0;
while(p){
// Record the total length of the linked list
i++;
p=p->next;
}
if(i==n){
// A special case Deleting the last element is the first element in positive order
head=head->next;
return head;
}
int j=i-n,k=1;// Last but not least N The first element is in positive order j A place
while(k<j){
r=r->next;
k++;
}
p=r->next;// Cover directly
r->next=p->next;
return head;
}
2. Use the speed pointer ( Double pointer ) Traverse , First use the fast pointer to move forward N Nodes , Then move the fast and slow pointer together , Until the fast pointer traverses to the last node , The slow pointer is the penultimate N+1 Nodes .
N + X =Length
// N It's the number of steps the pointer takes at the beginning ,X Indicates the number of steps followed by the fast and slow pointers .
// The slow pointer only goes X Step , also N The first step is not finished , That is to say, we have reached the penultimate N+1 Nodes .
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre=new ListNode(0);// Set a virtual node
pre->next=head;
ListNode* fast=pre;// Quick pointer
ListNode* slow=pre;// Slow pointer
while(n--){
// Let the pointer go first N Step
fast=fast->next;
}
while(fast->next){
// Go with the hands
slow=slow->next;
fast=fast->next;
}
ListNode* tmp=slow->next;// Form a good habit of releasing nodes
slow->next=slow->next->next;
delete tmp;// Form a good habit of releasing nodes
return pre->next;// Notice the returned representation
}
Don't look, there are two while Just traversed twice , Look carefully, the fast pointer only traverses once , Of course, it can be put in a while in , The purpose of writing is to make it easier for readers to understand .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011674.html
边栏推荐
- MySQL best practices for creating tables
- Fact final variable and final variable
- How does MySQL convert stored seconds into dates
- Understanding and installing MySQL
- JDBC connection database
- [leetcode169] most elements
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- Delete and truncate
- Code neat way to learn
- JDBC operation transaction
猜你喜欢
线性代数第三章-矩阵的初等变换与线性方程组
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
St table template
Contrôle automatique (version Han min)
MySQL table constraints and table design
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Explain of MySQL optimization
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
[leetcode 202] happy number
SQL injection
随机推荐
Contrôle automatique (version Han min)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
SQL optimization best practices
Filebrowser realizes private network disk
C language file operation
Fundamentals of digital image processing (Gonzalez) I
In depth source code analysis servlet first program
Integration and induction of knowledge points of automatic control principle (Han min version)
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
2. Devops sonar installation
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
Collections multiple parameter sorting
Viewer: introduce MySQL date function
[leetcode217] there are duplicate elements
Gaussian processes of sklearn
常用编程记录——parser = argparse.ArgumentParser()
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
POI and easyexcel exercises