判断给定的链表中是否有环。如果有环则返回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. python+selenium+pycharm使用

    上一章节讲了安装python及selenium,该章节讲解下使用pycharm编辑器进行代码编写 下载地址:http://www.jetbrains.com/pycharm/download/#sec ...

  2. 推荐 5 个 yyds 的开源 Python Web 框架

    提到 Python 的 Web 框架,第一反应就是老三样,Django,Flask 和 Tornado.如果按流行度来排名的话,应该也是这个顺序. 在 2016 年,发布了一款 Web 框架,叫 Sa ...

  3. 用端口映射的办法使用矩池云隐藏的vnc功能

    矩池云隐藏了很多高级功能待用户去挖掘. 租用机器 进入jupyterlab 设置vnc密码 VNC_PASSWD="userpasswd" ./root/vnc_startup.s ...

  4. PhpStorm中绘画UML

    IDE支持 在Plugins中 安装PlantUML integration插件 到http://www.graphviz.org/网站下载graphviz.exe并安装(这个软件可以支持更多的UML ...

  5. git常用命令及问题

    Git基本操作 git init 创建新的git仓库 git clone [url] 使用 git clone 拷贝一个 Git 仓库到本地 git status 查看工作区 git stash li ...

  6. netty系列之:netty中各不同种类的channel详解

    目录 简介 ServerChannel和它的类型 Epoll和Kqueue AbstractServerChannel ServerSocketChannel ServerDomainSocketCh ...

  7. MATLAB绘制一幅中国地图

    今天博主跟大家讲一下如何用MATLAB制作一幅中国地图,那废话不多说,我们先看一下最终效果吧. mercator墨卡托圆柱投影地图 lambert兰伯特圆锥投影地图 一张中国地图大概包括以下要素: 中 ...

  8. Paypal标准支付对接

    提醒一下,题主是在快速标准支付做到一半的时候换成了标准支付,所以该文档的快速支付大家做个参考就可以了. 一.两种支付方式 标准支付 优点:纯前端对接,简单方便,适用于非技术开发人员.个人即可用,不用花 ...

  9. 马哥教育Linux网络班结业考试(架构师)-简答题题目(附答案)

    1.叙述 centos7 启动图形界面的开机启动流程? 答:新版本的CentOS7里,已经做了调整.具体/etc/inittab 文件的第7行已经做出了说明: 系统已经使用'targets' 取代了运 ...

  10. [源码解析] TensorFlow 分布式环境(4) --- WorkerCache

    [源码解析] TensorFlow 分布式环境(4) --- WorkerCache 目录 [源码解析] TensorFlow 分布式环境(4) --- WorkerCache 1. WorkerCa ...