Leetcode 线性表 Linked List Cycle
2014-05-13 16:34
375 查看
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Total Accepted: 17041 Total
Submissions: 48975
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:判断一个链表中是否有环
思路:快慢指针,如果有环,最终快慢指针会在非NULL相遇
注:用到fast->next前先要确保fast非NULL,要用fast->next->next前先要确保fast,fast->next非NULL
复杂度:时间O(n), 空间O(1)
相关题目:Linked List CycleII
Linked List Cycle
Total Accepted: 17041 TotalSubmissions: 48975
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:判断一个链表中是否有环
思路:快慢指针,如果有环,最终快慢指针会在非NULL相遇
注:用到fast->next前先要确保fast非NULL,要用fast->next->next前先要确保fast,fast->next非NULL
复杂度:时间O(n), 空间O(1)
相关题目:Linked List CycleII
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { ListNode *fast, *slow; fast = slow = head; while(fast && fast->next){ fast = fast->next->next; slow = slow->next; if(fast == slow) return true; } return false; }
相关文章推荐
- LeetCode|Linked List Cycle II
- LeetCode 142: Linked List Cycle II
- LeetCode(141)(142) Linked List Cycle I II
- Linked List Cycle II -LeetCode
- leetcode--Linked List Cycle--判断链表是否有环
- leetcode -- Linked List Cycle -- 重点
- [LeetCode] Linked List Cycle II
- leetcode :Linked List Cycle
- Leetcode_141_Linked List Cycle
- leetcode(142). Linked List Cycle II
- Linked List Cycle——LeetCode
- LeetCode Linked List Cycle 解答程序
- LeetCode142:Linked List Cycle II
- [leetcode]Linked List Cycle @ Python
- [LeetCode] Linked List Cycle
- Leetcode#142 Linked List Cycle II
- LeetCode 142 — Linked List Cycle II(C++ Java Python)
- LeetCode:Linked List Cycle II
- LeetCode Linked List Cycle II
- leetcode -- Linked List Cycle