当前位置:网站首页>Leetcode19. Delete the penultimate node of the linked list [double pointer]
Leetcode19. Delete the penultimate node of the linked list [double pointer]
2022-07-07 22:49:00 【Qingshan's green shirt】
LeetCode19. Delete the last of the linked list N Nodes
List of articles
1. subject

2. Ideas
1. My own thinking is very simple and rough , Namely :
(1) Find the length of the linked list (2) Change the reciprocal into a positive number by calculation (3) Delete node
Pay attention to the special judgment header !! It is convenient to use sentinel nodes to handle header nodes !!
2. Simplified edition —— With two pointers , Just one scan
The following are implemented respectively .
3. Code implementation
(1) Reverse sequence to positive sequence
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int size = 0;// Chain length
ListNode* dummyHead = new ListNode(-1,head);// Sentinel node
ListNode* p = dummyHead;
while(p->next!= NULL)// First traversal : Statistical list length
{
p = p->next;
size++; // Chain length
}
int a = size-n+1;// The location of the positive order of the node to be deleted from 1 Start !!
if(a == 1)// Special judgment head node
{
ListNode*q = head;
dummyHead->next = head->next;
head = head->next;// Connecting nodes
delete q;
delete dummyHead;
return head;
}
else// Other situations
{
ListNode *q = dummyHead;
// Start from the sentinel node Just go The positive order of the node to be deleted -1 Step by step
int cnt = size-n;
while(cnt--)// Second traversal
q = q->next;
ListNode *m = q->next;
q->next = m->next;// Connecting nodes
delete m;
delete dummyHead;
return head;
}
}
};
matters needing attention
1. Pay attention to the topic condition :n It must be size Within the scope of
2. A little summary , I feel like I think about it every time I'm here , It's a bit of a waste of time :
Statistical list length , If you start from the sentinel node, usewhile(p->next!= NULL)
Starting from the beginning iswhile(p != NULL)
(2) Double pointer
The most important thing is to omit the steps of calculating the length of the linked list !! There is no need to judge the head node !!
Implementation point :1. Sentinel node 2. Double pointer
See notes for ideas :
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(-1,head) ;
ListNode* fast = dummyHead;// Quick pointer
ListNode* slow = dummyHead;// Slow pointer
int a = n+1;
//1. Let's go first n+1 Step —— In order to synchronize the time in the later stage slow Point to the previous node to be deleted , Convenient operation
while(a--)
fast = fast->next;// Back ward n+1 Step
//2. The speed pointer moves synchronously ,
while(fast!=NULL)
{
slow = slow->next;
fast = fast->next;
} // here slow Point to the previous node of the node to be deleted
//3. Delete operation
ListNode*p = slow->next;
slow->next = p->next;
delete p;
return dummyHead->next; // Note that there !
}
};
1. Special attention should be paid to the last sentence
return dummyHead->next;
Because the original head node head May have been deleted , The new head node is dummyHead->next.
2.n The value of must be reasonable , So don't think too much ! There will be no empty fingers !
边栏推荐
- Robot autonomous exploration series papers environment code
- Revit secondary development - modify wall thickness
- How to close eslint related rules
- 新版代挂网站PHP源码+去除授权/支持燃鹅代抽
- 23. Merge K ascending linked lists -c language
- Select sort (illustration +c code)
- Debezium系列之:支持 mysql8 的 set role 語句
- Dbsync adds support for mongodb and ES
- Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
- CTF练习
猜你喜欢

ASP.NET Core入门五
![[problem] pytorch installation](/img/9f/1419c471838e3af8ea2158266254a5.jpg)
[problem] pytorch installation

【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)

Ueeditor custom display insert code
![[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process

PKPM 2020 software installation package download and installation tutorial
![[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)](/img/35/1bb21c100980eb1075dbbcb922e181.png)
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)

Matplotlib快速入门

Redis cluster installation

Antd date component appears in English
随机推荐
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
Blender exchange group, welcome to the water group ~
0-5vac to 4-20mA AC current isolated transmitter / conversion module
Install mxnet GPU version
Relationship between URL and URI
Kaggle-Titanic
PHP method of obtaining image information
Get the week start time and week end time of the current date
What does it mean to prefix a string with F?
Px4 autonomous flight
Build an "immune" barrier in the cloud to prepare your data
Remember aximp once Use of exe tool
OpenGL configure assimp
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Aspose. Word operation word document (II)
6-3 find the table length of the linked table
变量与常量
Unity development --- the mouse controls the camera to move, rotate and zoom
Leetcode206. Reverse linked list
数字化转型:五个步骤推动企业进步