您的位置:首页 > 其它

[LeetCode]Linked List Cycle

2015-07-26 00:22 453 查看
解题思路:

题目的意思,就好像是在问,一个数组中,有没有重复的数字。因为 如果list中有环,那么必然存在两个指针指向同一个地址。

/**
* 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) {
set<ListNode*> hashMap;

ListNode* cur = head;
while(cur != NULL){
if (!hashMap.insert(cur).second){
return true;
}

cur = cur->next;
}

return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: