Linked List Cycle II
2017-02-28 10:42
197 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *p=findPoint(head),*q=head; if(!p) return NULL; while(p!=q){ p=p->next; q=q->next; } return p; } //有环的情况,找到第一次相碰点 ListNode *findPoint(ListNode *head){ if(!head) return NULL; ListNode *fast=head,*slow=head; while(fast&&fast->next){ slow=slow->next; fast=fast->next->next; if(slow==fast) return slow; } return NULL; } };
相关文章推荐
- LeetCode.142(141) Linked List Cycle && Linked List Cycle (II)
- LeetCode Linked List Cycle II
- Leetcode_linked-list-cycle-ii
- [Leetcode] Linked List Cycle II
- Linked List Cycle II
- 【LeetCode】Linked List Cycle II
- leetcode 刷题之路 59 Linked List Cycle II
- 142. Linked List Cycle II
- Leetcode: Linked List Cycle II
- leetcode -- Linked List Cycle II
- leetcode之Linked List Cycle II
- Linked List Cycle II
- *Linked List Cycle II
- Linked List Cycle II
- LeetCode:Linked List Cycle && Linked List Cycle II
- linked-list-cycle-ii
- Linked List Cycle II
- [LeetCode] Linked List Cycle II
- [Leetcode]Linked List Cycle II
- Linked List Cycle II