141. Linked List Cycle
来源:互联网 发布:xbox软件是什么 编辑:程序博客网 时间:2024/06/06 03:59
Given a linked list, determine if it has a cycle in it.
class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head) return false;
ListNode* slow=head;
ListNode* fast=head->next;
while(fast!=slow&&fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
if(fast==slow)
return true;
else
return false;
}
};
呵呵哒!又是1.88!老子太失落!
学习大佬的代码:
bool hasCycle(ListNode *head)
{
ListNode *fast;
fast = head;
while (head)
{
head = head->next;
if (fast->next && fast->next->next)
fast = fast->next->next;
else
return false;
if (fast == head)
return true;
}
return false;
}
这个速度快一点,不过没啥大区别
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- cpp——类——VTBL
- phpcms v9 创建模块
- cpp——类——VBTBL
- Linux下管道的实现机制
- 字符串格式化方式及递归
- 141. Linked List Cycle
- html5<canvas操作像素之反相、窗帘、模糊特效>
- cpp——类——RTTI
- Linux命令行修改IP、网关、DNS的方法
- Mac10.12允许任何来源如何显示
- 数据异常排查系统化方法
- linux设备驱动归纳总结(三):4.ioctl的实现
- (c++)orm框架简单对比
- 详解设计模式——装饰器模式