/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *one, *two,*oneTmp;
if (!head || !head->next)
return NULL;
one = head; two = head; oneTmp = head;
while (two && two->next )
{
one = one->next;
two = two->next->next;
if (two && one==two)
{
while (one)
{
if (one == oneTmp)
return one;
one = one->next;
oneTmp = oneTmp->next;
}
}
}
return NULL;
}
};
【LeetCode】Linked List Cycle II
最新推荐文章于 2018-05-26 09:17:45 发布