Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

题目大意:给定一个链表,判断是否有环?

解题思路:

解法一:快慢指针,如果有环,那么快慢指针总会相遇,有环;否则快的遍历完整个链表,无环。

解法二:HashSet保存节点,如果下一个节点在set中出现过,那么有环,否则直到遍历完整个链表,无环。

Talk is cheap>>

解法一:

    public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
if (slow == fast) {
return true;
}
}
return false;
}

解法二:

    public boolean hasCycle2(ListNode head) {
if (head == null || head.next == null) {
return false;
}
Set<ListNode> set = new HashSet<>();
while (head != null) {
if (set.contains(head)) {
return true;
}
set.add(head);
head = head.next;
}
return false;
}

Linked List Cycle——LeetCode的更多相关文章

  1. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

  2. Linked List Cycle leetcode II java (寻找链表环的入口)

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  3. Linked List Cycle leetcode java (链表检测环)

    题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...

  4. Linked List Cycle - LeetCode

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  5. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  6. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  7. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  9. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

随机推荐

  1. StructureMap Exception Code: 207 Internal exception while creating Instance '06fc8bd7-76db-47c1-8d71-31090a074f5e' of PluginType QIMS.Repository.IComStaffRepository. Check the inner exception for more

    标题翻译: StructureMap异常代码:207内部异常,同时创造PluginType QIMS.Repository.IComStaffRepository的实例“06fc8bd7-76db-4 ...

  2. weex APIs

    1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...

  3. RSA签名验签

    import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...

  4. JAVAWEB 生成excel文字在一格显示两位不变成#号

    在用java生成excel的时候会发现这种问题, 如果是人家给的模板还好,如果不是模板,而是通过代码生成的话, 就需要进行处理了, 一个小单元格,如果是一位的话,如1-9显示没有问题,一旦是两位的话, ...

  5. Java-Android 之单选按钮的运用

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. CSS控制鼠标滑过时的效果

    用css控制鼠标样式的语法如下:<span style="cursor:*">文本或其它页面元素</span>把 * 换成如下15个效果的一种: 下面是对这 ...

  7. Tomcat-java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory

    在我的MyEclipse中新建一个网站,并新建一个.jsp文件,配置server为Tomcat后,运行.jsp文件的时候,报错:java.lang.ClassNotFoundException: or ...

  8. Alljoyn 概述(1)

    Alljoyn Overview Feb. 2012- AllJoyn 是什么? • 2011年2月9日发布,由 QuiC(高通创新中心)开发维护的开源软 件项目,采用 Apache license ...

  9. iOS单例的两种实现

    单例模式算是开发中比较常见的一种模式了.在iOS中,单例有两种实现方式(至少我目前只发现两种).根据线程安全的实现来区分,一种是使用@synchronized,另一种是使用GCD的dispatch_o ...

  10. 服务器上搭建spark开发环境

    1.安装相应的软件 (1)安装jdk 下载地址:http://www.Oracle.com/technetwork/java/javase/downloads/index.html (2)安装scal ...