判断给定的链表中是否有环。如果有环则返回true,否则返回false。

解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断fast==slow或者fast.next==slow即可判断

 class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} public class test1 {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null){
//头指针为空或者只有头节点,无环
return false;
}
ListNode slow,fast = new ListNode(0);
slow = head.next;
fast = head.next.next;
while(true){
if(fast==null||fast.next==null){
//fast走到链表尾
return false;
}else if(fast.next==slow || fast==slow){
return true;
}else{
slow = slow.next;// slow每次走一步
fast = fast.next.next;//fast每次走两步
}
}
} public static void main(String[] args) {
ListNode node1 = new ListNode(1),node2 = new ListNode(2),node3 = new ListNode(3),node4=new ListNode(4);
node1.next=node2;
node2.next=node3;
node3.next=node4;
node4.next=node1;
test1 test = new test1();
System.out.println(test.hasCycle(node1));
}
}

判断链表是否有环(Java实现)的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  2. leetCode-linkedListCycle判断链表是否有环

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

  3. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  4. [Leetcode] Linked list cycle 判断链表是否有环

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

  5. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  6. [leetcode]141. Linked List Cycle判断链表是否有环

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

  7. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  8. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  9. python判断链表是否有环

    思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点: 假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点 ( ...

随机推荐

  1. 一台电脑设置多个网段的IP地址

                    

  2. 动态线程池(DynamicTp)之动态调整Tomcat、Jetty、Undertow线程池参数篇

    大家好,这篇文章我们来介绍下动态线程池框架(DynamicTp)的adapter模块,上篇文章也大概介绍过了,该模块主要是用来适配一些第三方组件的线程池管理,让第三方组件内置的线程池也能享受到动态参数 ...

  3. 阿里云CND加速

    1: :2: 3: 4: 5: 6: 7:将解析信息如实添加 8:如果报错添加 CNAME 记录提示和 A 记录冲突,也就是说如果你要添加 CDN 全站加速,域名解析那里就不能再有 A 记录了, 只有 ...

  4. php 23种设计模型 - 代理模式

    代理模式(Proxy) 在代理模式(Proxy Pattern)中,一个类代表另一个类的功能.这种类型的设计模式属于结构型模式. 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口. 介 ...

  5. composer.json和composer.lock到底是什么以及区别?

    composer方文档:https://docs.phpcomposer.com/04-schema.html我们在做项目的时候,总是要安装一些依赖.composer给我们提供了很多方便.直接运行co ...

  6. 微信小程序结合原生JS实现电商模板(一)

    前几天遇到一个朋友求助,实现购物车的相关功能,一时心血来潮,想着抽空搭建一个小程序电商平台(虽然网上有很多,但还是自己撸一遍才是王道),所以在工作之余整了一个仓库,今天提交了第一次代码,已经满足了朋友 ...

  7. Web网站建站过程(白嫖)——域名

    目录 1.域名注册商(选一个吧) 2.域名注册 没有域名建啥站? 1.域名注册商(选一个吧) 到时候你们就会想起: ...... 但是我们不用上面的,因为上面的太费Q,我们要用的是-- 2.域名注册 ...

  8. python3鸡兔同笼问题

    # 假设兔子有x只 for x in range(1,31): y = 30 - x if 4*x + 2*y == 90: print('兔子有%d只,鸡有%d只'%(x, y))

  9. Debian与Ubuntu到底有什么不同,应该如何选择?

    镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 在CentOS转向CentOS Stream之后,这意味着它将变得不可靠. 但是幸好,仍然有非常优秀的Linux发行版本在等我们.其中比较有知名度 ...

  10. 选择Key-Value Store

    [IT168 专稿]在之前的文章中,给大家介绍了<Redis快速入门:Key-Value存储系统简介>,今天进一步给大家介绍为什么选择Key-Value Store.Key-Value S ...