当前位置:网站首页>Sword finger offer special assault edition day 7
Sword finger offer special assault edition day 7
2022-07-24 04:55:00 【hys__ handsome】
The finger of the sword Offer II 021. Delete the last of the linked list n Nodes
Simple linked list preprocessing length , You can also use the stack or Double pointer ( i i i Go ahead n n n individual , then i i i And j j j Walk together until i i i To null pointer ) To do it
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;
}
};
The finger of the sword Offer II 022. The entry node of a link in a list
Hash table practices
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;
}
};
Fast and slow pointer practice
f a s t fast fast Take two steps , s l o w slow slow Take a step , until f a s t fast fast And s l o w slow slow Meeting shows that there are links in the linked list .
When we meet , Through mathematical relations s l o w slow slow To ring entry point +n The distance of the ring is just equal to the distance from the chain head to the ring entry point
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;
}
};
The finger of the sword Offer II 023. The first coincident node of two linked lists
- t1 Walk the A, go B
- t2 Walk the B, go A
Situation 1 、 Disjoint
They will all arrive at the same time nullptr And end the cycle .
Situation two 、 The intersection
Finally, we reach the intersection at the same time
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;
}
};
边栏推荐
- LeetCode 46 全排列
- Which is better, Xiaomi current treasure or yu'e Bao? Detailed comparison and introduction of the differences between Xiaomi current Bao and Alibaba yu'e Bao
- power. The operation is in the low peak period of business. Import call will help you prepare each word
- EMQX 简单使用
- 本,降低线上要度是一样的。发现异常实例cp操
- Common cross domain problems
- P一个配置文件期间将SDA松集成。支但事实上
- 最大公约数
- How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
- Design of high frequency small signal resonant amplifier course design Multisim Simulation
猜你喜欢

最大公约数

pso和mfpso

激活函数和最常用的10个激活函数

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

Little black leetcode journey: 100 same trees

Division of training set, verification set and test set in link prediction (take randomlinksplit of pyg as an example)

-Bash: wget: command not found

HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier

MapReduce concept

"Paper reproduction" bidaf code implementation process (3) model establishment
随机推荐
What if the computer desktop gets stuck? Introduction of solutions to computer crash and desktop jamming
黑色的的一站式运维管家 10条记录RO
Event extraction and documentation (2019)
mapreduce概念
Basic syntax of MySQL DDL and DML and DQL
Xiaomi finance was officially launched today (May 11) with a free 10000 yuan experience fee attached to the official address
Esp32:arduino tutorial summary
Uniapp learning
Several common sorts
Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in
Chapter 7 other neural network types
Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction
PSO and mfpso
EMQX 简单使用
Chapter III encog workbench
C语言:冒泡排序法
C语言:随机数的生成
HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
The software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
激活函数和最常用的10个激活函数