题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=196&tqId=37047&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=undefined&judgeStatus=undefined&tags=&title=
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode* p = pHead;
while (p->next) {
if(p->next->val <= p->val){
//说明有环
return p->next;
}
p = p->next;
}
return nullptr;
}
};
非常简单的几行代码,只要找到某个结点的下一个结点的值是否大于当前结点就知道了
查看18道真题和解析