Linked List Cycle
来源:互联网 发布:2016年汽车销售数据 编辑:程序博客网 时间:2024/06/05 15:48
c++
/** * 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) { if (head == nullptr || head->next == nullptr) return false; unordered_set<ListNode *> cache; while (head != nullptr) { if (cache.find(head) != cache.end()) return true; cache.insert(head); head = head->next; } return false; }};
python
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head or not head.next: return False fast = slow = head while slow and fast and fast.next: fast = fast.next.next slow = slow.next if(fast == slow): return True return False
reference:
https://leetcode.com/discuss/106782/in-place-python-code-beats-90%25
0 0
- leetcode Linked List Cycle & Linked List Cycle ||
- Linked List Cycle II Linked List Cycle
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle II
- [LeetCode]Linked List Cycle
- Linked List Cycle
- LeetCode | Linked List Cycle
- 电商之梳理UML相关知识-------建模使用
- C++之派生类的拷贝构造与赋值运算符重载
- Android 贝赛尔曲线实现自定义button 果冻-压下形变,抬起后弹弹的效果
- 【BZOJ4602】齿轮,带权并查集
- 初探66AK2x
- Linked List Cycle
- 原型模式深复制浅复制
- 机房收费系统总结(四)
- DatePicker&&TimePicker In DialogFragment
- 史上最奇葩的5个编程语言,有个甚至是模拟奶牛
- 排序算法总结
- bzoj 1231 [Usaco2008 Nov]mixup2 混乱的奶牛
- 更改shader的编译环境。
- Tomcat虚拟主机的配置