定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针必定会相遇。node
/** * 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;
}
};
复制代码
用 unordered_map 充当散列表的功能,每次将链表的节点指针做为键值存入 map,若是检测到当前节点指针已经存在于 map 中则说明链表有环。ui
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_map<ListNode *, char> nodemap; // 散列表功能
ListNode *temp = head;
while (temp)
{
if (nodemap.count(temp) == 1) return true; // 当前节点已存在于 map 中,则说明有环
nodemap[temp] = '0';
temp = temp->next;
}
return false;
}
};
复制代码
获取更多精彩,请关注「seniusen」! spa