当前位置:网站首页>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;
}
};
边栏推荐
- Retrofit擴展閱讀
- Tidb3.0- 4.0 memory control / modification log saving days / maximum index length
- Improve code checking with annotations
- Adapt to the pits in Huawei models
- Summary of mobile application development
- [DB written interview 220] how to back up control files in oracle? What are the ways to back up control files?
- 4.4 Eval function replaces function
- Retrofit Extended reading
- Markdown rule for writing articles
- Talking about Festinger effect
猜你喜欢

【MGT】代码解读之model-MGT

日記(C語言總結)

Storage of floating point numbers in C language in memory

Tsinghua University | van: visual attention network

STL tutorial 3- type conversion static_ cast、dynamic_ cast、const_ cast、reinterpret_ Cast method

Character function and string function

Two image enhancement methods: image point operation and image graying

安装MySQL出现白页面怎么办

客户端建设及调优实践

Can you implement these requirements with MySQL
随机推荐
在64位机器使用CMake编译32位程序
解密FTP
Retrofit扩展阅读
写文章的markdown规则
Detailed analysis of abstractqueuedsynchronizer (AQS) source code - Analysis of lock release and response interrupt locking processes
Inline functions in kotlin
Talking about Festinger effect
Unity中.Meta文件作用详解
sql查看数据库/表磁盘占用情况,杀死进程终止tidb中的连接
TiDB3.0- 4.0 内存控制/修改日志保存天数/最大索引长度
日記(C語言總結)
Tidb3.0- 4.0 memory control / modification log saving days / maximum index length
Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
About SQL: field association between two tables
适配华为机型中出现的那些坑
Gql+nodejs+mysql database
Introduction to testing - Software Test Model
finally block can not complete normally
Leedcode 1 - sum of two numbers
doc常用语法,更新中……