Linked List Cycle II
2013-12-16 19:23
148 查看
这篇文章讲得很清楚了。
http://fisherlei.blogspot.com/2013/11/leetcode-linked-list-cycle-ii-solution.html
/**
* 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 *fast = head, *slow = head;
while (fast != NULL && slow != NULL) {
fast = fast->next;
slow = slow->next;
if (fast != NULL)
fast = fast->next;
if (fast == slow)
break;
}
if (fast == NULL)
return NULL;
fast = head;
while (fast != slow) {
fast = fast->next;
slow = slow->next;
}
return fast;
}
};
http://oj.leetcode.com/problems/linked-list-cycle-ii/
http://fisherlei.blogspot.com/2013/11/leetcode-linked-list-cycle-ii-solution.html
/**
* 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 *fast = head, *slow = head;
while (fast != NULL && slow != NULL) {
fast = fast->next;
slow = slow->next;
if (fast != NULL)
fast = fast->next;
if (fast == slow)
break;
}
if (fast == NULL)
return NULL;
fast = head;
while (fast != slow) {
fast = fast->next;
slow = slow->next;
}
return fast;
}
};
http://oj.leetcode.com/problems/linked-list-cycle-ii/
相关文章推荐
- JAVA-Linked List Cycle I&&Linked List Cycle II
- Linked List Cycle II
- 【leetcode】【单链表】【142】Linked List Cycle II
- LEETCODE: Linked List Cycle II
- 1分钟 - Linked List Cycle II
- leetcode 142: Linked List Cycle II
- [LeetCode#142]Linked List Cycle II
- [leetcode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- Leetcode Linked List Cycle II
- [leetcode]142 Linked List Cycle II
- 38:Linked List Cycle II
- [LeedCode OJ]#142 Linked List Cycle II
- leetcode:142. Linked List Cycle II(Java)解答
- LeetCode-142 Linked List Cycle II
- Linked List Cycle II
- LeetCode Linked List Cycle II
- LeetCode:Linked List Cycle II
- leetcode-141-142 Linked List Cycle I II
- leetcode系列(5)-- Linked List Cycle II