当前位置:网站首页>leetcode:19. 删除链表的倒数第 N 个结点
leetcode:19. 删除链表的倒数第 N 个结点
2022-06-21 08:30:00 【OceanStar的学习笔记】
题目来源
题目解析


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;
}
};
快慢指针
先检测参数:如果链表为空或者K值小于1,此时,参数无效,直接返NULL。
然后先明确一点,如果要删除链表的某个节点,实际上是需要找到要删除节点的前一个节点,因此我们可以用快慢指针找到指定位置的节点
我们可以设想假设设定了双指针 p 和 q 的话,当 q 指向末尾的 NULL,p 与 q 之间相隔的元素个数为 n 时,那么删除掉 p 的下一个指针就完成了要求。
- 设置虚拟节点 dummyHead 指向 head
- 设定双指针 p 和 q,初始都指向虚拟节点 dummyHead
- 移动 q,直到 p 与 q 之间相隔的元素个数为 n
- 同时移动 p 与 q,直到 q 指向的为 NULL
- 将 p 的下一个节点指向下下个节点

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;
}
};
边栏推荐
- 4.6 lodash usage documents
- Gql+nodejs+mysql database
- 4.4 Eval function replaces function
- Unity .net 框架问题
- Global and Chinese market for diamond blades 2022-2028: Research Report on technology, participants, trends, market size and share
- Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
- 2022-2028 global hydrogen internal combustion engine industry research and trend analysis report
- Tidb and MySQL modify system variables / common statements (kill the process in process)
- 日記(C語言總結)
- Inline functions in kotlin
猜你喜欢

Audio immersive experience

2022-2028 global hydrogen internal combustion engine industry research and trend analysis report

2022-2028 global boom cylinder industry research and trend analysis report

Tsinghua University | van: visual attention network

window10局域网共享文件夹流程

Client construction and Optimization Practice

Linux安装达梦数据库/DM8(附带客户端工具安装完整版)

声临其境 — 音频沉浸体验

2022-2028 global postoperative pressure suit industry research and trend analysis report

Journal (résumé en langue c)
随机推荐
tidb4.0.0遇见的问题、报错总结(tiup部署)
Global and Chinese market for crankshaft position sensors 2022-2028: Research Report on technology, participants, trends, market size and share
【MGT】代码解读之model-MGT
Ads Filter Design Wizard tool I
LeetCode数组经典题目(一)
Visual studio code annotation plug-in: korofileheader
SQL to check the disk usage of the database / table, kill the process and terminate the connection in tidb
写文章的markdown规则
Vision_ Transformer code exercise
Unity write multithreading considerations
Two image enhancement methods: image point operation and image graying
Generic functions in kotlin
[early knowledge of activities] list of recent activities of livevideostack
[DB written interview 367] in the three-level schema structure of the database, the description of all data logical structures and features in the database is ()
JUnit5单元测试
Unity中.Meta文件作用详解
Doc common syntax, updating
Dumpling备份数据库
MySql 过滤查询(以字母开头,以数字开头,非数字开头,非字母开头)
【元宇宙3d大赛】