当前位置:网站首页>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 !
边栏推荐
- 【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
- Unity technical notes (I) inspector extension
- Unity technical notes (II) basic functions of scriptableobject
- 苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
- Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- 客户案例|华律网,通过观测云大幅缩短故障定位时间
- php 获取图片信息的方法
- Digital transformation: five steps to promote enterprise progress
- Record a garbled code during servlet learning
猜你喜欢
Remember an experience of using selectmany
行测-图形推理-7-相异图形类
Unity FAQ (I) lack of references
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
Quick sort (diagram +c code)
What does it mean to prefix a string with F?
0-5VAC转4-20mA交流电流隔离变送器/转换模块
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
随机推荐
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
100million single men and women "online dating", supporting 13billion IPOs
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
PKPM 2020 software installation package download and installation tutorial
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
Matplotlib quick start
Use partial derivatives to display normals in unity
Unity technical notes (I) inspector extension
Two methods of calling WCF service by C #
Unity local coordinates and world coordinates
OpenGL configure assimp
[interview arrangement] 0211 game engine server
如何选择合适的自动化测试工具?
How to choose the appropriate automated testing tools?
Revit secondary development - cut view
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
Relationship between URL and URI
Antd date component appears in English
How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
筑起云端 “免疫”屏障,让你的数据有备无患