当前位置:网站首页>leetcode:19. Delete the penultimate node of the linked list
leetcode:19. Delete the penultimate node of the linked list
2022-06-21 08:43:00 【Oceanstar's learning notes】
Title source
title


struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {
}
ListNode(int x) : val(x), next(nullptr) {
}
ListNode(int x, ListNode *next) : val(x), next(next) {
}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int k) {
return head;
}
};
Speed pointer
Test parameters first : If the linked list is empty or K Less than 1, here , Invalid parameter , Go straight back NULL.
Then make it clear , If you want to delete a node of the linked list , In fact, you need to find the previous node to delete , So we can use Speed pointer Find the node at the specified location
We can imagine that we have two pointers p and q Words , When q At the end of NULL,p And q The number of elements separated is n when , Then delete it p The next pointer of is done .
- Set the virtual node dummyHead Point to head
- Set double pointer p and q, Initially, they all point to virtual nodes dummyHead
- Move q, until p And q The number of elements separated is n
- Move at the same time p And q, until q Point to NULL
- take p The next node of points to the next node

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int k) {
if(head == nullptr || k < 0){
return head;
}
ListNode *dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode *slow = dummyHead;
ListNode *fast = dummyHead;
for (int i = 0; i < k + 1; ++i) {
fast = fast->next;
}
while (fast){
fast = fast->next;
slow = slow->next;
}
ListNode *delNode = slow->next;
slow->next = delNode->next;
delete delNode;
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
边栏推荐
- 日記(C語言總結)
- Figure out how MySQL works
- [DB written interview 366] stored procedures are codes stored in the database and have many advantages. Among the following statements, which are not the advantages of stored procedures are ()
- Retrofit擴展閱讀
- Unity .net 框架问题
- FD:文件描述符
- Unity的网络请求_短连接
- STL教程3-类型转换static_cast、dynamic_cast、const_cast、reinterpret_cast方法
- Mono fourni avec l'unit é 5 peut également supporter C # 6
- Audio immersive experience
猜你喜欢

Two image enhancement methods: image point operation and image graying

Use lua+redis+openresty to realize concurrent optimization of e-commerce Homepage

tidb4.0.0遇见的问题、报错总结(tiup部署)

Tsinghua University | van: visual attention network

智能制造的下一站:云原生+边缘计算双轮驱动
![[Mgt] model Mgt for code interpretation](/img/4f/694e7b93626436007021b718ad69b5.png)
[Mgt] model Mgt for code interpretation

Classic topics of leetcode array (I)

Unity write multithreading considerations

【活动早知道】LiveVideoStack近期活动一览
![[early knowledge of activities] list of recent activities of livevideostack](/img/8c/f8007931b1a5944f3a0a243a5afcc4.png)
[early knowledge of activities] list of recent activities of livevideostack
随机推荐
GQL+Nodejs+MySQL数据库
4.9 commander. js
WordPress obtains category name and link through category ID
Client construction and Optimization Practice
[DB written interview 220] how to back up control files in oracle? What are the ways to back up control files?
Storage of floating point numbers in C language in memory
[Mgt] model Mgt for code interpretation
Represent each record in the dataframe as a dictionary
WordPress media library supports uploading and previewing SVG icons
JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
客户端建设及调优实践
Difference between function declaration and function expression
5 minutes to understand MySQL - row to column
C # implement callback
Markdown rule for writing articles
Give two strings S and T, and judge whether T is the word formed after rearrangement of S
adb使用技巧和usb通信原理
Unity .net 框架问题
Unity中.Meta文件作用详解
Extensions in kotlin