题目:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:
给定一个链表,检测该链表是否是循环链表
Follow up:
是否能够不使用额外内存空间解决。
思路:
1、最容易想到的方法是,用一个哈希表unordered_map<int, bool> visited,记录每个元素是
否被访问过,一旦出现某个元素被重复访问,说明存在环。空间复杂度O(n),时间复杂度O(N)。
2、最好的方法是时间复杂度O(n),空间复杂度O(1) 的。设置两个指针,一个快一个慢,快
的指针每次走两步, 慢的指针每次走一步, 如果快指针和慢指针相遇, 则说明有环。
代码:12ms
/** * 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* slow = head; ListNode* fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if(slow==fast){ return true; } } return false; } };