class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL || head->next == NULL){
return NULL;
}
ListNode *first = head;
ListNode *second = head->next;
while(first != second){
if(second==NULL || second->next==NULL){
return NULL;
}
else{
first = first->next;
second = second->next->next;
}
}
first = head;
second = second->next;
while(first!=second){
first = first->next;
second = second->next;
}
return first;
}
};
142. Linked List Cycle II
最新推荐文章于 2022-02-09 22:06:59 发布