当前位置:网站首页>Sword finger offer II 022 The entry node of the link in the linked list
Sword finger offer II 022 The entry node of the link in the linked list
2022-04-23 05:30:00 【Mikawa】
subject : Given a linked list , Return to the first node of the link where the list begins to enter . Start from the head node of the linked list next The first node where the pointer enters the ring is the entry node of the ring . If the list has no links , Then return to null.
Answer key : Use the speed pointer to determine whether there is a ring , When there is a ring, quickly return the pointer to the chain header , Then the two pointers move one step at the same time , It is the entry node when they meet again
Code
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode* fast = pHead, *slow = pHead, *ptr = pHead;
while (NULL != fast) {
if (NULL == fast->next) {
break;
}
slow = slow->next;
fast = fast->next->next;
if(slow == fast) {
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return slow;
}
}
return NULL;
}
};
版权声明
本文为[Mikawa]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220543093933.html
边栏推荐
- egg测试的知识大全--mock、superTest、coffee
- 3d slicer中拉直体的生成
- JS time format conversion
- Create a tabbar component under the components folder, which is public
- Qwebsocket communication
- Arithmetic and logical operations
- QSS, qdateedit, qcalendarwidget custom settings
- How to set the initial value of El input number to null
- CPT 104_TTL 09
- egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
猜你喜欢
随机推荐
Generation of straightening body in 3D slicer
Qwebsocket communication
FileReader API file operation
史上最强egg框架的error处理机制
Uncle wolf is looking for a translator -- Plato -- ongoing translation
QSS, qdateedit, qcalendarwidget custom settings
d.ts---更详细的知识还是需要看官网的介绍(声明文件章节)
Fast application fuzzy search
How to realize adaptive layout
Create process memory management copy_ Mm - processes and threads (IX)
Laravel routing job
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
Ehcache Memcache redis three caches
巴普洛夫与兴趣爱好
The title bar will be pushed to coincide with the status bar
uni使用的一些坑
MySQL series - install MySQL 5.6.27 on Linux and solve common problems
The QT debug version runs normally and the release version runs crash
Data bus realizes the communication between brother components
Click the Add button - a box appears (similar to adding learning experience - undergraduate - Graduate)









