单链表是否有环的问题解决与讨论(java实现)
单链表是否有环的问题经常在面试中遇到,一般面试中会要求空间为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实现)的更多相关文章
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 如何判断单链表是否存在环 & 判断两链表是否相交
给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...
- [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 ...
- [算法][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 ...
- C# 判断一个单链表是否有环及环长和环的入口点
1.为什么写这个随笔? 前几天参加一个电面,被问到这个问题,想总结一下. 2.为什么标题强调C#? 想在网上看看代码,却没找到C#版的,于是自己用C#实现一下. 一.解决问题的思路 1.一种比较耗空间 ...
- 判断单链表是否有环,并找出环的入口python
1.如何判断一个链表是否有环? 2.如果链表为存在环,如果找到环的入口点? 1.限制与要求 不允许修改链表结构. 时间复杂度O(n),空间复杂度O(1). 2.思考 2.1判断是否有环 如果链表有环, ...
- [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 ...
- [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 ...
随机推荐
- Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)
RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...
- android(十四)四种启动模式
standard 启动的activity会每次都重新创建一个activity放到任务栈中.这是系统默认的启动模式. singleTop启动的activity,如果任务的栈顶刚好存在当前的activit ...
- Python数据结构:列表、字典、元组、集合
列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...
- uva11404
这题说的是给了一个长度为n的字符串(1000)求最长回文子序列,并输出当str[i]==ste[j]时dp[i][j]=dp[i+1][i-1]+2 否则 dp[i][j]=Max(dp[j+1][i ...
- Python ConfigParser的使用
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- HTTP从入门到入土(1)——五层网络模型
五层网络模型 简介 互联网的实现,依托于网络协议.网络协议又分为好几层,关于如何分层有过很多争论,比较受人认可的有五层模型.七层模型.四层模型.今天我们就来讲讲五层网络模型. 从名字就可以看出来,五层 ...
- git中Untracked files如何清除
$ git status # On branch test # Untracked files: # (use "git add <file>..." to inclu ...
- ubuntu 16.04下更换源和pip源【转】
本文转载自:https://blog.csdn.net/weixin_41500849/article/details/80246221 写在前面的话 本文主要内容是更换系统源为清华大学源,更换pyt ...
- ubuntu 18.04 64bit没有声音如何解决
一.背景 1.1 笔者的机器有两张声卡,使用aplay -l可以列举出来,一张是内置声卡,另一张是显卡自带的声卡,说明声卡驱动是ok的 1.2 笔者是在浏览器中播放视频无声音 二.尝试 2.1 尝试使 ...
- 如何在 OSX 中使用多个JDK版本
升级macbook小白的硬盘成SSD后,重新安装了系统和JDK8,但是启动eclipse还是报告需要安装JDK6,于是也按照提示安装了Apple JDK6,这导致系统中有两个JDK,一个是Oracle ...