[leetcode] Linked List Cycle
2015-06-03 02:33
316 查看
From : https://leetcode.com/problems/linked-list-cycle/
Given a linked list, determine if it has a cycle in it.
Given a linked list, determine if it has a cycle in it.
/** * 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) return false; ListNode *p1=head, *p2=head->next; while(p2) { p1 = p1->next; if(p2->next) { p2=p2->next->next; } else return false; if(p1 == p2) return true; } return p1==p2; } };
相关文章推荐
- 【安卓学习之路1】轻轻一点,好神奇——给activity_main.xml添加Button控件
- [LeetCode] Word Ladder II 词语阶梯之二
- 机器学习-贝叶斯
- [leetcode] Kth Largest Element in an Array
- 【最小生成树】Bzoj1601[Usaco2008 Oct]灌水
- [转]Xcode 的正确打开方式——Debugging
- HackerRank - "Detect HTML Attributes"
- Sqrt(x)
- SQLHelper类
- Valid Number
- [hdu5247]rmq+预处理
- [hdu5249]动态中位数
- 网络请求 代码 系统自带类源码
- Gas Station
- 【强连通分量】Bzoj1051 HAOI2006 受欢迎的牛
- iOS CGRectGetMaxX/Y 使用
- Reverse Integer 解题报告
- 图 | 为什么存在关于图的研究
- [hdu4497]分解质因数
- jQuery 属性