leetcode Linked List Cycle & Linked List Cycle ||
2014-08-01 17:28
190 查看
如果知道链表环的相关前提知识,则较为方便就可以求出结果,具体分析可以查看博客/article/4814004.html
代码
Linked List Cycle
Linked List Cycle ||
代码
代码
Linked List Cycle
class Solution{ public: bool hasCycle(ListNode * head) { ListNode *slow, *fast; if(head == NULL||head->next==NULL) return false; slow = head; fast = head->next; while(slow->next!=NULL&&fast->next&&fast->next->next!=NULL) { if(slow==fast) return true; slow = slow ->next; fast = fast->next->next; } return false; } };
Linked List Cycle ||
代码
class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *slow, *fast; if(head==NULL||head->next==NULL) return NULL; slow = head; fast = head; bool flag = false; while(slow->next!=NULL&&fast->next!=NULL&&fast->next->next!=NULL) { slow = slow->next; fast = fast->next->next; if(slow == fast) { flag = true; break; } } if(flag) { ListNode *p; p = head; while(p!=slow) { p = p->next; slow = slow ->next; } return p; } else return NULL; } };
相关文章推荐
- LeetCode Linked List Cycle & Linked List Cycle II题解
- LeetCode Linked List Cycle & Linked List Cycle II
- LeetCode: Linked List Cycle I && II
- leetcode--Linked List Cycle &Linked List Cycle II
- 【LeetCode】Linked List Cycle 解题报告(Java & Python)
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- 【LeetCode】Linked List Cycle I&II
- <leetcode系列> Linked List Cycle II
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
- LeetCode:Linked List Cycle && Linked List Cycle II
- leetcode day5 -- Reorder List && Linked List Cycle II
- Leetcode | Linked List Cycle I && II
- 【leetcode】Linked List Cycle I & II
- leetcode之linked List cycle && linked List cycle II
- LeetCode Linked List Cycle && Linked List CycleII
- LeetCode:Linked List Cycle && Linked List Cycle II
- [leetcode] 141& 142 Linked List Cycle I& II
- [Leetcode][JAVA] Linked List Cycle I && II
- <leetcode系列> Linked List Cycle
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环