LeetCode 141. Linked List Cycle
2016-07-13 19:53
232 查看
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解法难道只有这样了吗?
Follow up:
Can you solve it without using extra space?
解法难道只有这样了吗?
/** * 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 || !head->next) return false; ListNode* fast = head; ListNode* slow = head; while(fast && fast->next && slow->next){ if(fast->next == slow) return true; fast = fast->next->next; slow = slow->next; } return false; } };
相关文章推荐
- CentOS下使用yum安装Git具体步骤
- 网络视频直播将给影视界带来重大影响
- 使用四种框架分别实现百万websocket常连接的服务器
- saltstack入门之一:saltstack安装
- Linux基础知识的学习(一)
- PLY文件格式及其MATLAB读写操作
- 使用Java实现的简易多窗口卖票程序
- Untiy Native Render Plugin在VR中的绘制(二): 透明排序
- jzoj 1391. 【2012.02.25普及组】危险系数
- Java Web学习(10):JSP结构与生命周期
- 欢迎使用CSDN-markdown编辑器
- 服务治理过程演进
- FastDFS + Nginx module
- 写给自己,梳理一下我现在对前端知识结构的理解
- SPOJ KGSS Maximum Sum
- 【BZOJ 4636】蒟蒻的数列
- Linux系统如何查看CPU型号等
- 二分查找
- java中的Timer和TimeTask
- 托管代码与非托管代码之间与托管程序