单链表是否有环的问题经常在面试中遇到,一般面试中会要求空间为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. MegaCli 监控raid状态 限戴尔服务器

    MegaCli 监控raid状态 MegaCli是一款管理维护硬件RAID软件,可以通过它来了解当前raid卡的所有信息,包括 raid卡的型号,raid的阵列类型,raid 上各磁盘状态,等等.通常 ...

  2. Java中参数传递时值传递的机制分析

    参数传递是什么?      在C的函数或是JAVA的方法中,向一个函数或方法内部传递一个参数,比如:   void fun( int num ){     num+=2 ; }   int a = 3 ...

  3. 【Python】如何取到input中的value值?

    练习:取到下方链接下所有海贼王的下载链接. # coding=utf-8 from selenium import webdriver from time import sleep import ke ...

  4. Python-装饰器-案例-获取文件列表

    import os def get_all_path(fun): '''装饰器.功能:获取全路径文件名.如:D:/tmp/12.txt :param fun: :return:file_path_li ...

  5. go-002-语言结构

    Go 语言的基础组成有以下几个部分: 包声明package,必须在源文件中非注释的第一行指明这个文件属于哪个包, 引入包import,在开头部位使用 import 导入包,单个包 import “fm ...

  6. arguments参数对象

    //求任意个数的和 function test() { var sum = 0; for (var i = 0; i < arguments.length; i++) { sum += argu ...

  7. MyBatis—mybatis-config.xml配置介绍

    在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码   说明: 收藏代码 1.  <bean id="sqlSessionFactory" ...

  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. linux第六周

    一.知识概要 进程的描述 进程描述符task_struct数据结构(一)进程描述符task_struct数据结构(二)进程的创建 进程的创建概览及fork一个进程的用户态代码理解进程创建过程复杂代码的 ...

  10. 如何利用Xshell在windows与linux之间互传文件

    如何利用Xshell在windows与linux之间互传文件 第一步: 安装Xshell. 第二步: 打开Xshell,若出现默认的对话框,则选择关闭,因为下面将演示如何将本地文件传输至远程linux ...