当前位置:网站首页>剑指offer专项突击版第7天
剑指offer专项突击版第7天
2022-07-22 18:34:00 【hys__handsome】
剑指 Offer II 021. 删除链表的倒数第 n 个结点
简单链表预处理长度,也可以用栈 或 双指针( i i i 先走 n n n 个,然后 i i i 与 j j j 一起走直到 i i i 到空指针)来做
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int sz = 0;
ListNode res(0,head), *t = head;
while(t){
sz++;
t = t->next;
}
t = head;
ListNode *tmp = &res;
for(int i = 0; i < sz-n; i++, t = t->next, tmp = tmp->next) ;
tmp->next = t->next;
return res.next;
}
};
剑指 Offer II 022. 链表中环的入口节点
哈希表做法
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *t = head, flag(0);
unordered_map<ListNode*,bool> um;
while(t) {
if(!um.count(t)) um[t] = true;
else return t;
t = t->next;
}
return nullptr;
}
};
快慢指针做法
f a s t fast fast 走两步, s l o w slow slow 走一步,直到 f a s t fast fast 与 s l o w slow slow 相遇说明链表存在环。
在相遇时,通过数学关系得到 s l o w slow slow 到环入口点+n圈环的距离刚好等于链表头到环入口点距离
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head, *slow = head;
while(fast) {
slow = slow->next;
if(fast->next == nullptr) return nullptr;
fast = fast->next->next;
if(fast == slow) {
auto ptr = head;
while(ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return nullptr;
}
};
- t1走完A,走B
- t2走完B,走A
情况一、不相交
他们最后都会同时到nullptr而结束循环。
情况二、相交
最后同时到达相交点
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *t1 = headA, *t2 = headB;
while(t1 != t2){
t1 = t1 == nullptr? headB: t1->next;
t2 = t2 == nullptr? headA: t2->next;
}
return t1;
}
};
边栏推荐
- NLP language model
- 数组-稀疏矩阵的转置
- MRS +Apache Zeppelin,让数据分析更便捷
- USACO data set (2022.07.22)
- 中兴通讯云基础设施开源与标准总监李响:面向企业的开源风险与开源治理
- USACO资料集(2022.07.22)
- 2020_ ACL_ A Transformer-based joint-encoding for Emotion Recognition and Sentiment Analysis
- centos7安装和卸载mysql5.7
- 7. 求100~300间能被3整除的数的和。
- yapi和Apifox 哪个好用?深度分析 yapi 和Apifox 的功能特性
猜你喜欢

Introduction to distributed learning and federated learning

Chapter 6.3: manual compilation of starrocks under arm architecture (expansion)

XML架构

Buuctf miscellaneous - QR code

How to configure a cute little shark theme for typera?

3步就能制作漫画头像的机器人,想拥有一个吗?

Redis cluster setup

Ropgadget -- ret2syscall

Intel(中国)云基础设施软件研发总监王庆:Intel在云原生里的技术发展和展望

Transformer
随机推荐
C51 single chip microcomputer digital (display hours, minutes and seconds)
2019_ AAAI_ Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
lasso回归结果美化
第6.3章:ARM架构下手动编译StarRocks(拓展篇)
Robot Arm 机械臂源码解析
Encoder decoder (seq2seq)
DB207-ASEMI整流桥一般用在什么地方,DB207参数尺寸
Introduction to 51 single chip microcomputer (dedicated to the most understandable article for beginners) update
[SUCTF 2019]EasySQL
华为首席开源联络官任旭东:深耕基础软件开源,协同打造数字世界根技术
2. 输入一个圆半径r,当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。
2020_ ACL_ A Transformer-based joint-encoding for Emotion Recognition and Sentiment Analysis
Conditions affectant la vitesse de requête de l'interface
狂神redis笔记09
接口文档进化图鉴, 用过第一款接口文档工具的人暴露年龄了
Chapter 6.3: manual compilation of starrocks under arm architecture (expansion)
图文并茂演示小程序movable-view的可移动范围
NLP学习路线图(思维导图),非常的全面和清晰!
R语言绘制日历热图
视频去除色彩,教你简单不复杂的操作方法