【LeetCode】Linked List Cycle
2013-10-29 22:21
411 查看
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
java code : 很简单的判断单链表是否有环。
Follow up:
Can you solve it without using extra space?
java code : 很简单的判断单链表是否有环。
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head == null || head.next == null) return false; ListNode p = head, q = head.next; while( q!= null && q.next != null) { if(p == q) return true; p = p.next; q = q.next.next; } return false; } }
相关文章推荐
- JBoss提供的常用的对称加密算法
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
- [World Wind学习]22.相机高度和瓦片等级计算
- C语言
- apple风格的html文档生成
- ffmpeg 学习 之几个基本数据结构之间联系
- (递归)汉诺塔
- iOS开发-小知识(二)
- HDU3468:A Simple Problem with Integers(线段树区间更新+lazy)
- IOS7 新特性(针对同样讨厌更新后IOS7的开发者)
- [置顶] bzoj 1036 树的统计Count 点权值模板
- Python Cookbook学习记录 ch3_3/4_2013/10/29
- Sublime Text 配置使用总结(不断更新....)
- SPOJ 8222. Substrings(后缀自动机模板)
- All in All 找子串 水题
- 三国演义 ----感悟
- fcntl的区域锁定
- HDU1698:Just a Hook(线段树区间更新)
- spinner自定义,效果如腾讯QQ账号选择时候的下拉列表
- 写给大家的编程书(2)