Linked List Cycle
来源:互联网 发布:淘宝专业打假 编辑:程序博客网 时间:2024/05/09 12:15
描述
Given a linked list, determine if it has a cycle in it.
Follow up: Can you solve it without using extra space?
分析:判断链表是否有环路
最好的方法是时间复杂度O(n),空间复杂度O(1) 的。设置两个指针,一个快一个慢,快
的指针每次走两步, 慢的指针每次走一步, 如果快指针和慢指针相遇, 则说明有环。
class Solution {public:bool hasCycle(ListNode *head) {// 设置两个指针,一个快一个慢ListNode *slow = head, *fast = head;while (fast && fast->next) {slow = slow->next;fast = fast->next->next;if (slow == fast) return true;}return false;}};
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
- kaggle数据挖掘——以Titanic为例介绍处理数据大致步骤
- LeetCode(213) House Robber II
- 【技巧】——将Chrome浏览器变为文本编辑器
- HDU5317.RGCDQ
- c++ 11 声明
- Linked List Cycle
- iOS9 CASpringAnimation 弹簧动画详解
- Ubuntu循环登录问题解决方案
- hdu杭电4006 The kth great number【优先队列】
- 解决Android端升级安装后自动打开的问题
- 实现文字渐变的效果
- Linux find 用法示例
- 软件测试方法和测试流程
- hdoj 1873 看病要排队 [STL 优先队列]