单链表是否有环的问题经常在面试中遇到,一般面试中会要求空间为O(1);再者求若有环,则求环产生时的起始位置。

下面采用java实现。

//单链表
class ListNode{
int val;
ListNode next;
ListNode(int x){
val=x;
next=null;
}
}
public class SearchCycleNode{
ListNode equalListNode=null;//来记录判断有环时出现的相等的时的节点,姑且叫"相遇"节点。
  //从"相遇"节点出发,第一个可达的节点(从单链表的头节点开始)即是单链表的环产生的起始位置
public ListNode detectCycle(ListNode head) {
if(!hasCycle(head)) return null;
ListNode p=head;
while(!isReach(p)){
p=p.next;
}
return p; }
//判断从相遇的节点到 head节点可达性
private boolean isReach(ListNode head){
if(head==equalListNode)return true;
ListNode p=equalListNode.next;
while(p!=equalListNode){
if(p==head)return true;
p=p.next;
}
return false;
}
//判断是否有环,通过一个指针p走一步,一个指针q走两步,如果能出现p=q的情况,则有环,并记录p为"相遇"节点。
private boolean hasCycle(ListNode head) {
if(head==null)return false;
if(head.next==null)return false;
ListNode p=head;
ListNode q=head.next;
while(p!=q){
if(p.next==null)return false;
p=p.next;
if(q.next==null)return false;
if(q.next.next==null)return false;
q=q.next.next;
}
equalListNode=p;
return true; }
}

单链表是否有环的问题解决与讨论(java实现)的更多相关文章

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

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

  2. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  3. 如何判断单链表是否存在环 & 判断两链表是否相交

    给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...

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

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

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. C# 判断一个单链表是否有环及环长和环的入口点

    1.为什么写这个随笔? 前几天参加一个电面,被问到这个问题,想总结一下. 2.为什么标题强调C#? 想在网上看看代码,却没找到C#版的,于是自己用C#实现一下. 一.解决问题的思路 1.一种比较耗空间 ...

  7. 判断单链表是否有环,并找出环的入口python

    1.如何判断一个链表是否有环? 2.如果链表为存在环,如果找到环的入口点? 1.限制与要求 不允许修改链表结构. 时间复杂度O(n),空间复杂度O(1). 2.思考 2.1判断是否有环 如果链表有环, ...

  8. [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 ...

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

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

随机推荐

  1. 傅里叶变换 VS 拉普拉斯变换

    拉普拉斯变换的公式 傅里叶变换公式 拉普拉斯变换是将时域映射到s plane上,而傅里叶变换实际是将时域 映射在s-plane的虚轴上, 傅里叶变换可以看作拉普拉斯变换  的一种特例 1.推导傅里叶变 ...

  2. 【Loadrunner】平台1.9环境APP成功录制并调试成功后的脚本备份

    1.录制相关Loadrunner及录制的APP所在手机网络代理相关设置请参考日志:http://www.cnblogs.com/zhuzhubaoya/p/9152022.html 2.调试成功的脚本 ...

  3. python selenium 安装与 chromedriver安装

    安装 pip install selenium 安装完成之后运行脚本,如果没报错那ok.但是很不幸运,我报错啦.(本人使用ubuntu16.04,python2,or python3) 贴出我的报错: ...

  4. python学习之路-day8

    一.接口与归一化设计 1.什么是接口 调用某个功能的方法/方式/入口 2.为什么要用接口 接口提取了一群类共同的函数,可以把接口当做一个函数的集合. 然后让子类去实现接口中的函数. 这么做的意义在于归 ...

  5. SQLServer cast()函数

    语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的数据,在AS ...

  6. SQLite 自定义函数,聚合,排序规则

    SQLite 自定义函数,聚合,排序规则 1.使用自定义函数, 聚合以及排序规则的基本方法是使用回调函数.这些注册的函数的生命周期只存在于应用程序中, 并不存储在数据库文件中, 因此需要在每个连接建立 ...

  7. pyDay13

    内容来自廖雪峰的官方网站. 1.把list.dict.str等Iterable变成Iterator可以使用iter()函数 >>> L = iter([1, 2, 3, 4, 5, ...

  8. 数据导入(二):MapReduce

    package test091201; import java.io.IOException; import java.text.SimpleDateFormat; import java.util. ...

  9. linux内核启动时报错ubi0 error: validate_ec_hdr: bad data offset 256, expected 128

    1.错误解析 ubi的EC header中有一个字段data_offset来记录数据偏移,数据偏移必须正确才能正确读取每一个物理擦除块中的数据 2.解决方法 擦除整块flash,然后再重新烧写包含ub ...

  10. [CF600E]Lomsat gelral

    题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 线段树合并板子题,没啥难度,注意开long long 不过这题$dsu$ $on$ $tre ...