linked-list-cycle
2015-07-04 09:01
323 查看
中等 带环链表
48%通过
给定一个链表,判断它是否有环。
您在真实的面试中是否遇到过这个题?
Yes
样例
给出 -21->10->4->5, tail connects to node index 1,返回 true
挑战
不要使用额外的空间
linked list cycle ii 不求环开始的位置:
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ public boolean hasCycle(ListNode head) { if(head == null || head.next == null){ return false; } ListNode slow = head; ListNode fast = head.next; while(fast != slow){ if(fast.next == null || fast.next.next == null){ return false; } fast = fast.next.next; slow = slow.next; } return true; } }
相关文章推荐
- [Angularjs]ng-show和ng-hide
- jsp request 获取路径
- gpu vs cpu
- [leetcode]Summary Ranges
- Express4--说明
- 黑马程序员----继承2(接口、多态、object)
- 人活着系列Tanya和蔡健雅猪 (floyd)
- Adb server didn't ACK, failed to start daemon
- install caffe
- 关于start-up routine 和 exec 和main()
- ZigBee产品认证指南
- TCP/IP详解学习笔记——数据链路层(2)
- BZOJ 1012 最大数maxnumber
- 第四章 深入类的方法
- Win10预览版10163截图曝光,隶属winmain分支
- 如何看待:java支持图形用户界面???
- ssh 即使主机,同nohup背景脚本
- Hadoop三种安装模式:单机模式,伪分布式,真正分布式
- 单循环链表的约瑟夫问题(C++)
- [UIImage resizableImageWithCapInsets:]使用注意