当前位置:网站首页>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 !
边栏推荐
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- Revit secondary development - modify wall thickness
- C # realizes the communication between Modbus protocol and PLC
- Remember an experience of using selectmany
- Build an "immune" barrier in the cloud to prepare your data
- C development -- WPF simple animation
- Amesim2016 and matlab2017b joint simulation environment construction
- IP network active evaluation system -- x-vision
- 行测-图形推理-3-对称图形类
- 如何选择合适的自动化测试工具?
猜你喜欢

The whole network "chases" Zhong Xuegao

C # realizes the communication between Modbus protocol and PLC

0-5vac to 4-20mA AC current isolated transmitter / conversion module

“拧巴”的早教行业:万亿市场,难出巨头

Matplotlib快速入门

Two methods of calling WCF service by C #

详解全志V853上的ARM A7和RISC-V E907之间的通信方式

如何选择合适的自动化测试工具?

Gazebo import the mapping model created by blender

ASP.NET Core入门五
随机推荐
What is the difference between the three values of null Nan undefined in JS
OpenGL configure assimp
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
How to choose the appropriate automated testing tools?
Build an "immune" barrier in the cloud to prepare your data
Unity technical notes (I) inspector extension
如何选择合适的自动化测试工具?
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Use blocconsumer to build responsive components and monitor status at the same time
Anti climbing killer
C development - interprocess communication - named pipeline
PHP method of obtaining image information
[environment] pycharm sets the tool to convert QRC into py file
How to write an augmented matrix into TXT file
How to realize the movement control of characters in horizontal game
Firefox browser installation impression notes clipping
Relationship between URL and URI
Pyqt GUI interface and logic separation
如何选择合适的自动化测试工具?
客户案例|华律网,通过观测云大幅缩短故障定位时间