Total Accepted: 95108
Total Submissions: 258001
Difficulty: Medium
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
我的AC(4ms,最快一批):
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *fast, *slow;
fast = slow = head;
while (fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
分析:
此题为判断一个链表是否有环,之前看过,没啥说的..题目要求“without using extra space”
相关题目:判断一个链表是否有环,如果有找出入口点。