您的位置:首页 > 其它

[LeetCode] Linked List Cycle II

2014-11-23 21:38 501 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
null
.
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *p = head;
ListNode *q = head;
while(q && q -> next){
p = p -> next;
q = q -> next ->next;
if(p == q)
break;
}
if(q == NULL || q -> next == NULL)
return NULL;
p = head;
while(p != q){
p = p -> next;
q = q -> next;
}
return p;
}
};


参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: